Filter löschen
Filter löschen

Loading .mat files with same variable name

16 Ansichten (letzte 30 Tage)
Peyman
Peyman am 13 Aug. 2015
Kommentiert: Titus Edelhofer am 14 Aug. 2015
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

Akzeptierte Antwort

Titus Edelhofer
Titus Edelhofer am 13 Aug. 2015
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 Kommentare
Peyman
Peyman am 14 Aug. 2015
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 am 14 Aug. 2015
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});

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Workspace Variables and MAT-Files finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by