userstack
userstack

Reputation: 1

How to save output into multiple txt file?

I have 100 images to analyze and I want each result of images will save into a file.So, I have 100 images and I want to have 100 txt file.Right now, it only can save the last result. Here is my code.

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);

I already change mode 'w' into 'a' so it appends the result but it still in the same file. How can I have different txt file for each input?

Upvotes: 0

Views: 24

Answers (1)

Shai
Shai

Reputation: 114786

you need to open a new file at each iteration.
discard the leading fid=fopen('Mycode.txt','w'); and trailing fclose(fid); and change your code to:

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

Upvotes: 2

Related Questions