writematrix: missing "newline" option...

22 visualizaciones (últimos 30 días)
JarryG
JarryG el 4 de Mayo de 2022
Comentada: Walter Roberson el 4 de Mayo de 2022
I have some script exporting data into textfile using "dlmwrite", but Matlab HelpCenter says "dlmwrite is not recommended, use writematrix instead". So I would like to convert my script, but how can I control line terminator with "writematrix"? I'm using "newline" option with "dlmwrite" frequently, because I have to move data between windows/linux...

Respuestas (1)

Walter Roberson
Walter Roberson el 4 de Mayo de 2022
writematrix() does not support any ways to configure the end of line.
However, if the issue issue is windows vs linux, then you should not worry about it, as the ways to read text almost all ignore carriage returns. load() and readmatrix() ignore carriage returns for example.
There are some commands that do not ignore carriage returns. If you fopen() a file then you can include the 't' access right, such as fopen(filename, 'rt') to make it explicit that you want carriage returns to be ignored.
  2 comentarios
Rik
Rik el 4 de Mayo de 2022
If you really have to, you can also read the resulting file and replace the line endings with the kind you need.
Walter Roberson
Walter Roberson el 4 de Mayo de 2022
S = fileread(NameOfCSV);
oldlen = length(S);
S(S == newline) = [];
if length(S) ~= oldlen
fid = fopen(NameOfCSV, 'w');
fwrite(fid, S);
fclose(fid);
end
The code might possibly have to change a little if the matrix contains unicode characters.

Iniciar sesión para comentar.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by