Tricky string to number
1 view (last 30 days)
Show older comments
Hello,
I'm writing a script in Matlab which is useful for running different CFX simulations. The latter provides automatically the name to the files of the form: "file_001", "file_002"... "file_00i". I am trying to give this name to some files but I cannot suceed. THis is my attempt:
name2= num2str(i,'%3.0')
ofile2 = strcat('file_',name2);
which gives file_
In addition I need to write on a file this sentence: "cfx5solve -def Sol_00i.res -ccl file_00i"
even in this case I am experiencing some problems. THIs is my attempt:
fprintf(fid1,'cfx5solve -def Sol_%3.0f.res -ccl file_%3.0f',i,i);
and it gives "cfx5solve -def Sol_ i.res -ccl file_ i"
Have you got any idea, please.
Thank you very much for your time!
p.s. i goes from 1 to 260
0 Comments
Accepted Answer
Walter Roberson
on 12 May 2011
ofile2 = sprintf('file_%03d',i);
or if you want to use the same method you started with,
name2 = num2str(i,'%03d');
What you were trying for the other line should have given you a number in the appropriate places as long as "i" was assigned a number (it defaults to being the square root of negative one)
fprintf(fid1,'cfx5solve -def Sol_%03d.res -ccl %s',i,ofile2);
3 Comments
Matt Fig
on 12 May 2011
Read the doc for FPRINTF or SPRINTF or both. All of the format specifiers are discussed in detial there:
doc fprintf
More Answers (0)
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!