我有一个这样的循环:
for i=1:no
%some calculations
fid = fopen('c:\\out.txt','wt');
%write something to the file
fclose(fid);
end
我希望将数据写入不同的文件:
i=1
,数据会写入out1.txt
i=2
,数据会写入out2.txt
i=3
,数据会写入out3.txt
执行'out'+ i
不起作用。 如何做到这一点?
答案 0 :(得分:5)
另一种选择是函数SPRINTF:
fid = fopen(sprintf('c:\\out%d.txt',i),'wt');
答案 1 :(得分:3)
filename = strcat('out', int2str(i), '.txt');
答案 2 :(得分:1)
你有没有尝试过:
int2str(i)
答案 3 :(得分:0)
更简单:
for i=1:no
%some calculations
fid = fopen(['c:\out' int2str(i) '.txt'],'wt');
%write something to the file
fclose(fid);
end
PS。我不相信Matlab字符串需要转义除了''(除非它是* printf样式函数的格式字符串)
编辑:见评论@MatlabDoug