execute a loop with diffrent name

1 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2015-2-25
编辑:Stephen23 2015-2-25
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by