MATLAB: Won’t \n give me a new line

fprintfwrite to text file

I am trying to write my data to a text file but the usual \n is not giving me a new line!
fid1 = fopen(filedesignation,'w');
fprintf(fid1,'%s, ',title1);
fprintf(fid1,'%4.2f, %4.2f, %4.2f, %4.2f, %4.2f\n',DataMatrix);
fclose(fid1);
When I try printing this to the command window, the \n works, but not when I try and write to a file. Why?!
How can I get a line return in my text file if this doesn't work?

Best Answer

It works perfectly when you write it to a file also. But the Windows NotePad ("Editor" in German) fails to display the linebreaks correctly. But Matlab's editor, Word, NotePad++, WordPad, (X)Emacs (under Linux and Windows), Alpha (on MacOS-9 !), BBEdit, vi, etc. display the \n line break correctly even without the \r.
When you open the file in the 'wt' mode, \n is converted to \r\n automatically, but this can have certain unexpected side effects. E.g. the number of characters obtained by ftell can differ from the number of bytes the file has. Reading by fgets will be slower, because all control characters are considered on the fly, e.g. backspaces and ^Z as end-of-file. In addition the created results differ between Windows and Linux. So I'd prefer to retire NotePad, install NotePad++ or WordPad as standard viewer of txt files and prefer the \n linebreaks as Matlab's editor does for several years now.