Tricky string to number

2 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2011-5-12
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 个评论
mortain Antonio
mortain Antonio 2011-5-12
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 2011-5-12
Read the doc for FPRINTF or SPRINTF or both. All of the format specifiers are discussed in detial there:
doc fprintf

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by