Filter löschen
Filter löschen

MATLAB not saving matrix as .txt correctly

3 Ansichten (letzte 30 Tage)
Max Mills
Max Mills am 18 Dez. 2015
Bearbeitet: Walter Roberson am 18 Dez. 2015
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?
ans=
0 0
0.1000 0.1010
0.2000 0.2080
0.3000 0.3270
0.4000 0.4640
0.5000 0.6250
0.6000 0.8160
0.7000 1.0430
0.8000 1.3120
0.9000 1.6290
I'm using
dlmwrite('first10_rows.txt',ans,'delimiter','\t','precision',3)
to save the file, which produces this in the text file
0 0
0.1 0.101
0.2 0.208
0.3 0.327
0.4 0.464
0.5 0.625
0.6 0.816
0.7 1.04
0.8 1.31
0.9 1.63
Thanks

Akzeptierte Antwort

Guillaume
Guillaume am 18 Dez. 2015
Bearbeitet: Guillaume am 18 Dez. 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')

Weitere Antworten (1)

Renato Agurto
Renato Agurto am 18 Dez. 2015
Bearbeitet: Renato Agurto am 18 Dez. 2015
  2 Kommentare
Guillaume
Guillaume am 18 Dez. 2015
Please format links as link (either use the link button or surround it with < and >)
Renato Agurto
Renato Agurto am 18 Dez. 2015
Done. Thanks!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by