Tricky string to number

3 views (last 30 days)
mortain Antonio
mortain Antonio on 12 May 2011
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

Accepted Answer

Walter Roberson
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
mortain Antonio
mortain Antonio on 12 May 2011
in case I have a number like 302930.0 how can I tell matlab to give me as output this number and not 3.0293e+005
Matt Fig
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

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown 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!