writematrix: missing "newline" option...
21 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2022-5-4
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 个评论
Rik
2022-5-4
If you really have to, you can also read the resulting file and replace the line endings with the kind you need.
Walter Roberson
2022-5-4
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!