Effacer les filtres
Effacer les filtres

Loading .mat files with same variable name

16 vues (au cours des 30 derniers jours)
Peyman
Peyman le 13 Août 2015
Commenté : Titus Edelhofer le 14 Août 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

Réponse acceptée

Titus Edelhofer
Titus Edelhofer le 13 Août 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 commentaires
Peyman
Peyman le 14 Août 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 le 14 Août 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});

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by