writematrix: missing "newline" option...
21 views (last 30 days)
Show older comments
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...
0 Comments
Answers (1)
Walter Roberson
on 4 May 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 Comments
Rik
on 4 May 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
on 4 May 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.
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!