MATLAB: How to print each matrix row on a separate line when using DLMWRITE in MATLAB 7.10 (R2010a)

eolMATLAB

I am trying to print a matrix to a text file using the DLMWRITE function as in the example below:
M = [1 ;2 ;3 ;4 ; 5]
dlmwrite('myfile.txt', M, 'delimiter', '\t');
The file is created correctly, but when I open it using Notepad, I see all the matrix elements printed on a single line rather than each element being printed on a separate line. How can I ensure that each row in the matrix is printed on a separate line?

Best Answer

'Notepad' does not recognise the end of line character used in DLMWRITE. If the file is opened using a different editor such as 'WordPad', the matrix will be displayed correctly with each row being displayed on a separate line.
If your code is to be run on a Windows machine, you can use the following syntax to ensure that the correct end of line character is used. This will guarantee that the file is displayed correctly regardless of the text editor used to open it:
dlmwrite('myfile.txt', M, 'delimiter', '\t','newline', 'pc');