execute a loop with diffrent name

1 visualización (últimos 30 días)
Rica
Rica el 25 de Feb. de 2015
Editada: Stephen23 el 25 de Feb. de 2015
Hi All,
the name of my data are :
data50_1.mat data50_2.mat......data50_100.
data86_1.mat data80_2.mat......data80_100.
and i have these loop
for k=1:100
A=struct2cell(load (['data50_' num2str(k) '.mat']));
end
My question How could i use the loop for data86 using some tricky indexing?
I have not only data50_... and data86_..., but i have more data set.
Thank you

Respuesta aceptada

Stephen23
Stephen23 el 25 de Feb. de 2015
Editada: Stephen23 el 25 de Feb. de 2015
You could do this in two loops using sprintf , something like this:
for k1 = [50,86]
for k2 = 1:100
file_name = sprintf('data%u_%u.mat',k1,k2);
load(file_name)
end
end
Currently your code will completely replace the data from the previous loop, as on every iteration it assigns new data to the variable A. If you wish to avoid this, then you need to use some indexing to store all of the data, or consider using a structure and dynamic field names to store the load data directly:
A = struct([]);
for k1 = [50,86]
for k2 = 1:100
file_name = sprintf('data%u_%u.mat',k1,k2);
A(k2).(sprintf('data%u',k1)) = load(file_name);
end
end
Structures have lots of other useful tools and features to make working with your data easy.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices 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