Loading .mat files with same variable name

16 次查看(过去 30 天)
I could do with some help.
I have loads of .mat structure arrays that contain 'data' (84x179). I want to be able to plot the data on top of each other but the problem is that since they are all called 'data', they replace each other in my workspace. What is a good way around this? Is there a way I can change 'data' for each .mat file or should I create a for loop. I don't know how to do this so any tips would be most appreciated.
Thanks

采纳的回答

Titus Edelhofer
Titus Edelhofer 2015-8-13
Hi,
the main idea is to use load with a return argument instead of as a command, something like
files = dir('*.mat');
allData = cell(size(files));
for ii=1:length(files)
aFileData = load(files(ii).name);
allData{ii} = aFileData.data;
end
Titus
  2 个评论
Peyman
Peyman 2015-8-14
Thanks Titus. I will try this and let you know how it goes. If I were to change the name of each variable in the .mat file, would it be an easy way of doing it?
Titus Edelhofer
Titus Edelhofer 2015-8-14
Yes, that would be no problem. If you know that it's only one variable, use fieldnames:
aFileData = load(files(ii).name);
% names of the variables stored:
variableNames = fieldnames(aFileData);
% use the first name and "dynamic field names", i.e., (...):
allData{ii} = aFileData.(variableNames{1});

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by