Borrar filtros
Borrar filtros

Loading .mat files with same variable name

16 visualizaciones (últimos 30 días)
Peyman
Peyman el 13 de Ag. de 2015
Comentada: Titus Edelhofer el 14 de Ag. de 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

Respuesta aceptada

Titus Edelhofer
Titus Edelhofer el 13 de Ag. de 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 comentarios
Peyman
Peyman el 14 de Ag. de 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 el 14 de Ag. de 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});

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Workspace Variables and MAT-Files en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by