Tricky string to number

조회 수: 2 (최근 30일)
mortain Antonio
mortain Antonio 2011년 5월 12일
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