execute a loop with diffrent name

1 visualizzazione (ultimi 30 giorni)
Rica
Rica il 25 Feb 2015
Modificato: Stephen23 il 25 Feb 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

Risposta accettata

Stephen23
Stephen23 il 25 Feb 2015
Modificato: Stephen23 il 25 Feb 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.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by