how to save loop result in a specified folder, where the folder name predefined according to the iteration parameter

조회 수: 4 (최근 30일)
Suppose I have the following code
U_range=20:25;
for i=1:6
U=U_range(i);
Result=U^2;
save('C:\Users\KKB\Documents\U=20\Result.mat','Result'); % Only for U=20,Modification required here ????
end
I want to save All Result (here six), in the specified path i.e.,C:\Users\KKB\Documents, but the folder name where the six Result will be saved are respectively U=20,U=21,U=22,U=23,U=24 and U=25. It should be noted that six empty folder having the above mentioned name are already created in the specified path. Only the Result need to be save in those respective folder. How to do that?

채택된 답변

David Barry
David Barry 2016년 12월 17일
편집: David Barry 2016년 12월 17일
You can use num2str on your loop iterator to build up the folder name. For example:
for ii = 1:3
a = rand(ii);
fdr = ['/Users/davidbarry/Documents/MATLAB/', 'U=', num2str(ii)];
if ~exist(fdr, 'dir')
mkdir(fdr);
end
save([fdr, '/Result.mat'], 'a');
end
By the way, I would avoid having equals symbol in the folder or file name but that's up to you.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by