MATLAB not saving matrix as .txt correctly
3 views (last 30 days)
I'm having some problems saving a matrix as a .txt file. I want to save the matrix as it appears in MATLAB, with columns. In the command screen, the matrix appears as normal (below), however, when I save it as a .txt, it doesn't seem to be able to define between columns and mixes all the numbers together in a single row. I have tried using '\t' and ' ' as delimiters but this isn't helping. I have also tried saving the matrix as a variable rather than ans. Does any one know how to solve this?
to save the file, which produces this in the text file
Guillaume on 18 Dec 2015
Edited: Guillaume on 18 Dec 2015
You must have been looking at the text file with notepad. Any more sophisticated text file reader (e.g. notepad++) would have shown you the file the way you expected.
I don't know if it's on purpose or a bug, but, in 2015b on windows, dlmwrite use unix style line endings instead of windows line ending. Most text editors don't care which one it is but lowly notepad only recognises windows line endings. Note that matlab does not care either, it'll read your text file just right regardless of the line ending.
To force dlmwrite to use windows line endings:
dlmwrite('first10_rows.txt',ans,'delimiter','\t','precision',3, 'newline', 'pc')