How to change in sequences of files each output of two for loops one inside the other
1 次查看(过去 30 天)
显示 更早的评论
Hello, I have too "for" loops one inside the other and I would like to save the output in sequences of files. I can do it successfully with one "for" loop, but I do not know how to do it for both With one "for" loop my code is as shown:
for n=0:50:100;
%there is some code here that calculates the variable RESULT%
...
%save files e.g n=0 to n=100
filename=['SF140profilen', int2str(n),'.txt'];
save(filename, 'RESULT', '-ascii');
end
This works, but now I need a loop inside, as shown:
for n=0:50:100;
for m=100:20:200
%there is some code here that calculates the variable RESULT%
%save files e.g n=0 to n=100
filename=['m100profilen', int2str(n),'.txt'];
save(filename, 'RESULT', '-ascii');
?
end
end
I would like to change this 100 in 'm100profilen' to give me the series of files produced buy the loop that changes m. Could you help me please? The names of the files should indicate the increasing values in n and m.
0 个评论
采纳的回答
dpb
2015-9-22
Just write a specific format string for the variables as needed...
filename=sprintf('m100profilen%03d%03d.txt', m,n);
NB: use of %03d to
a) create a constant width numeric field and
b) include leading zeros so will sort in numeric order as well as lexically
6 个评论
dpb
2015-10-2
Surely....btw, I'm particularly fond of the dir solution for such cases as it avoids having to build file names entirely other than the one wild card instance. Of course, it relies on the somewhat limited facility in what wildcard syntax is supported (and for some unfathomable reason, TMW hasn't implemented even all of the capability available even under Windows command processor :( ). But, one can still use a more expansive wild card matching and the use regexp or similar on the results to filter.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!