Reading mat files with variables that have different names

Hi there,
I am trying to load a bunch of mat files, the contents of which are CELL variables. For example when I load a file named "part1_240_T_1200_sig_25.mat", I get a variable named new_X1 which is a cell of size 1 by 240.........I tried various things like calling it in a structure, loading it like new_X = load('part1_240_T_1200_sig_25.mat'), but I still cant automate the process. At the end, I woud like to load the files in a loop (which I have done) but also change the variable names dynamically as the loop goes through several mat files.
So, if I could get something like
for ii = 1:files_length
eval(['load ' files(ii).name ])
num = size(new_X*{"ii"}*,2); %......change the cell variable name with the loop index
end
Would num2str work?? Thanks

 Respuesta aceptada

Image Analyst
Image Analyst el 25 de Mayo de 2012
Editada: Image Analyst el 2 de En. de 2019
Just do something like this:
for ii = 1:files_length
% Get filename
filename = fullfile(pwd, files(ii).name)
% Load the file into a structure.
storedStructure = load(filename);
% Extract the new_X1 into a vector. We expect that it's a scalar.
% Otherwise use
% new_X{loopIndex} = storedStructure.new_X1;
% to put it into one cell of a cell array.
new_X(loopIndex) = storedStructure.new_X1;
% Now do something with new_X in this loop.
end
You're reading in the files incorrectly (by using eval). See the FAQ for some correct ways to read in the various files in a loop: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F. While you're at it, read this FAQ entry also: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Más respuestas (2)

Jan
Jan el 25 de Mayo de 2012
Avoid eval(). Whenever you think of using this evil function, I promise, that there is a better, nicer, faster and more reliable method. (Phew, the frequent readers of the forum know, that even I know one task, which can be solved by eval() only - beside the evalulation of symbolic expressions).
eval(['load ' files(ii).name ])
==>
load(files(ii).name);
But it is even better to catch the loaded values in a struct:
Data = load(files(ii).name);

2 comentarios

you answer is incorrect because it jump the sequence of the foor loop. So you correct it and post again.
See my (Image Analyst) answer above. I added a bunch of comments that will hopefully explain it.

Iniciar sesión para comentar.

TUSHAR ANDRIYAS
TUSHAR ANDRIYAS el 25 de Mayo de 2012
Both methods work like a charm.....that is exactly wat I wanted.....Thanks guys

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Mayo de 2012

Comentada:

el 2 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by