MATLAB: How can I start a new line when I write in an existing text file while being in a loop?

formatspectext file

Hi,
When I use this code I get at the end all my results on the same line in my final text file. This is obviously not what I expected. But I cannot solve this problem. If I add more %f, the code doesn't write anything in the text file and if I replace \n by \r it writes all the results on the same column, which is the same problem at the end (I also tried different combinations with \n and \r, I always get the same result). The matrix C is a matrix with 5 columns and 1 line and is replaced at every iteration by an other matrix C with different values. The new values are supposed to be written in the text file, on an other line, etc..
while go
A=inputSingleScan(s);
v = [toc i];
C = horzcat(A,v);
Fid=fopen('results.txt','a');
fprintf(Fid,'%f\n',C);
clear A v C
i=i+1;
end
Thank you very much

Best Answer

fid = fopen('test.txt','w') ;
for i = 1:5
A = rand(5) ; v = rand(5,1) ;
C = horzcat(A,v);
fprintf(fid,[repmat('%f\t', 1, size(C, 2)) '\n'], C') ;
fprintf(fid,'\n') ;
%dlmwrite('test.txt',C,'-append')
end
fclose(fid) ;
Related Question