Friday, December 04, 2009

MATLAB fprintf end of line carriage return line feed

I fought for a while with Matlab trying to write lines of text, with an end of line \n, when opening the text file with notepad it failed to enter a carriage return (cr), so I got a single long line of numers.

This is a well know issue with Windows. You have to open the file for write or append in a special "windows mode", as follows:

fid=fopen(fid,'wt'); % 'wt' is the mode that works in windows, not 'w'
% or fid=fopen(fid,'at'); % 'at' to append to an existing file

for i=1:3
fprintf(fid,'%g \n',3.141519);

produces a text file with:


No comments:

Post a Comment