Matlab 如何将输出保存到多个txt文件中?
我有100张图像要分析,我希望每个图像的结果都会保存到一个文件中。所以,我有100张图像,我希望有100个txt文件。现在,它只能保存最后的结果。 这是我的密码Matlab 如何将输出保存到多个txt文件中?,matlab,Matlab,我有100张图像要分析,我希望每个图像的结果都会保存到一个文件中。所以,我有100张图像,我希望有100个txt文件。现在,它只能保存最后的结果。 这是我的密码 fid=fopen('Mycode.txt','w'); for k = 1:nColors numTotalImage = (size(a,1) * size(a, 2))*3; %151287 numnonzero = nnz(segmented_images{k}); Percentage
fid=fopen('Mycode.txt','w');
for k = 1:nColors
numTotalImage = (size(a,1) * size(a, 2))*3; %151287
numnonzero = nnz(segmented_images{k});
Percentage = (numnonzero /numTotalImage)*100;
A = cluster_center(k,1);
B = cluster_center(k,2);
m =[k; A ;B ;Percentage];
fprintf(fid , '%.1f, %f, %f, %.1f \r\n' , m);
end
fclose(fid);
我已经将模式“w”更改为“a”,因此它会附加结果,但仍在同一文件中。如何为每个输入设置不同的txt文件?您需要在每次迭代时打开一个新文件。
丢弃前导的
fid=fopen('Mycode.txt','w')代码>和尾随fclose(fid)代码>并将代码更改为:
for k=1:nColors
fid = fpoen(sprintf('Mycode_color%d.txt',k),'w'); %// different file according to k
% do your stuff here...
fprintf(fid , '%.1f, %f, %f, %.1f \r\n' , m); %// write to file
fclose(fid); %// close the file at each iteration
end
您需要在每次迭代时打开一个新文件。
丢弃前导的fid=fopen('Mycode.txt','w')代码>和尾随fclose(fid)代码>并将代码更改为:
for k=1:nColors
fid = fpoen(sprintf('Mycode_color%d.txt',k),'w'); %// different file according to k
% do your stuff here...
fprintf(fid , '%.1f, %f, %f, %.1f \r\n' , m); %// write to file
fclose(fid); %// close the file at each iteration
end