如何将字符串文件名包含在fopen函数中?请参阅下面的代码和评论。
for i=1:5
filename = strcat('Cali',num2str(i));
%first iteration: filename = Cali1
%instead of result.txt there should be Cali1.txt in the following statement, but i want to achieve this by using the string filename
fid = fopen( 'results.txt', 'wt' );
fprintf( fid, 'write something');
fclose(fid);
end
答案 0 :(得分:2)
这是基本的Matlab功能。您应该更仔细地阅读本手册。也就是说,这是您需要的代码:
for i=1:5
fid = fopen(['Cali' num2str(i) '.txt'], 'wt');
fprintf(fid, 'write something');
fclose(fid);
end
如果您想使用strcat
,只需在上面的代码中添加行filename = strcat('Cali', num2str(i), '.txt');
即可。