开发者

How do I create a string using a loop variable in MATLAB?

I have a loop like this:

for i=1:no

  %some calculations

  fid = fopen('c:\\out.txt','wt');
  %write something to the file
  fclose(fid);

end

I want data to be written to different files like this:

  • for i=1, data is written to out1.txt
  • for i=2, data is written to out2.txt
  • for i=3, data is written to out3.txt
  • etc开发者_运维问答.

Doing 'out'+ i does not work. How can this be done?


Yet another option would be the function SPRINTF:

fid = fopen(sprintf('c:\\out%d.txt',i),'wt');


filename = strcat('out', int2str(i), '.txt');


Did you try:

int2str(i)


More simply:

for i=1:no
  %some calculations
  fid = fopen(['c:\out' int2str(i) '.txt'],'wt');
  %write something to the file
  fclose(fid);

end

PS. I don't believe Matlab strings need escaping except for '' (unless it's a format string for *printf style functions)

EDIT: See comment @MatlabDoug

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜