How to iterate a structure name and save directory in a for loop?

조회 수: 15 (최근 30일)
I am trying to use a for loop that saves parameters for 10 different files into a structure, but I am not sure how to get the name of each structure to iterate. My final result should be 10 different structures saved into a directory with the name for the structures being rate100,rate150,rate200,..... So far my for loop has two issues. The first is I am unable to use ['rate' num2str(cnt)] for the structure name, and the second is I am unsure how to get the save path to be my pathname + 'rate' + #. How can I change my for loop to get the structure name to iterate and why does my format for the save command not work?
saveDir = 'my/path/name
cnt = 50;
for j=1:10
cnt = cnt+50;
['rate' num2str(cnt)].minTime = tmin;
['rate' num2str(cnt)].maxTime = tmax;
% Save structure
save([saveDir '/rate' num2str(cnt) '.mat'],['rate' num2str(cnt)]);
end
  댓글 수: 1
Stephen23
Stephen23 2023년 1월 22일
편집: Stephen23 2023년 1월 22일
"...with the name for the structures being rate100,rate150,rate200,..... So far my for loop has two issues."
The main issue is that you are forcing meta-data into variable names.
Forcing meta-data into variable names is usually a sign that you are doing something wrong:
If you simply stored that meta-data inside the structure and used exactly the same variable names in every MAT file, then your code would be simpler, more efficient, and much more robust:
saveDir = 'my/path/name';
for k = ..
S.minTime = tmin;
S.maxTime = tmax;
S.rate = whatever_meta_data_you_want;
% Save structure
F = [saveDir,'/rate',num2str(cnt),'.mat'];
save(F, 'S'); % simpler and much more robust
end
In contrast, your approach of forcing meta-data into the variable names suffers from a number of problems:
(that explanation is for fieldnames, but it applies just as much to forcing meta-data into variable names)

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2023년 1월 22일
saveDir = 'my/path/name
for j=1:10
cnt = 50 * (j+1);
clear savestruct
ratefield = "rate" + cnt;
savestruct.(ratefield).minTime = tmin;
savestruct.(ratefield).maxTime = tmax;
filename = fullfile(saveDir, ratefield + ".mat");
save(filename, '-struct', 'savestruct');
end
Notice this contains no dynamic variable names -- but it does contain dynamic field names within a structure.
When you save a structure with the -struct option, then instead of saving the struct as a variable, it saves each field of the struct as a variable.

추가 답변 (1개)

Voss
Voss 2023년 1월 22일
saveDir = 'my/path/name';
cnt = 50;
for j=1:10
cnt = cnt+50;
S = struct('minTime',tmin,'maxTime',tmax);
% Save structure
filename = fullfile(saveDir,sprintf('rate%d.mat',cnt));
save(filename,'-struct','S');
end
  댓글 수: 5
Voss
Voss 2023년 1월 22일
Yes, but the file name is the same as the structure name, and it'll be a struct when she load()s it.
Stephen23
Stephen23 2023년 1월 22일
Alternative avoiding the intermediate structure:
for k = ..
..
save(filename,'tmin','tmax');
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by