Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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?

Réponse acceptée

David Barry
David Barry le 17 Déc 2016
Modifié(e) : David Barry le 17 Déc 2016
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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by