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

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