필터 지우기
필터 지우기

execute a loop with diffrent name

조회 수: 1 (최근 30일)
Rica
Rica 2015년 2월 25일
편집: Stephen23 2015년 2월 25일
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