How to change in sequences of files each output of two for loops one inside the other

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.

 Accepted Answer

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 Comments

Thanks dbd, Some files are as I expect. But I am getting some files, for example, as follows:
SF100profileCW1801.500000e+01.txt
SF100profileCW1803.000000e+01.txt
SF100profileCW1805.500000e+01.txt
SF100profileCW1806.000000e+01.txt
SF100profileCW1808.500000e+01.txt
and I am not getting for example, as I would expect the files:
SF100profileCW180015.txt
SF100profileCW180030.txt
SF100profileCW180055.txt
SF100profileCW180060.txt
SF100profileCW180085.txt
How could I fix this? Could you please tell me? thanks, Katerina
ERRATUM Prior comment in error; see follow-up. You inadvertently got a '%e' in the format string for the second field...
Hi, I used what you suggested:
filename=sprintf('SF100profileCW%03d%03d.txt', m,n);
So the code is:
for n=0:5: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 and m...
filename=sprintf('SF100profileCW%03d%03d.txt', m,n);
save(filename, 'RESULT', '-ascii');
end
end
How I could modify this to work? Could you please let me know? I would also like that the name is in the form:
SF100profileCW1800.txt
SF100profileCW1805.txt
SF100profileCW18010.txt
SF100profileCW18015.txt
etc
that is that n appears in the name as 0,5,10,15 etc not as 000,005,010,015. (So that I can then upload a sequence as
filename = ['SF100profileCW180', int2str(w), '.txt'];)
thanks
That does work...
It's not possible from the above code to have generated the sequence 180015 ff so clearly the above code isn't identical to that which caused the problem. In it you must have had a typo
>> m=180;n=85;
sprintf('SF100profileCW%03d%03e.txt', m,n)
ans =
SF100profileCW1808.500000e+01.txt
>>
NB: the 'e' instead of 'd' in the formatting string. The two keys are only separated by the row so not too unlikely a mistype particularly if have fingernails... :)
ADDENDUM
I missed the second part, sorry...
I'd recommend against doing that; there's a loss of both sorting in order numerically as well as lexical plus it's possibly ambiguous as to what the interpretation of the values are. Instead use
filename=[num2str(w,'SF100profileCW180%03d'),'.txt'];
or
d=dir('SF100profileCW180*.txt'];
and iterate over d().name
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.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 22 Sep 2015

Commented:

dpb
on 2 Oct 2015

Community Treasure Hunt

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

Start Hunting!