Tricky string to number
1 次查看(过去 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
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!