我有一个名为temp
的数组,其中包含double
- 尺寸为240×20×10428的精度值。我想把它写到一个文本文件中。我尝试了以下方法:
dlmwrite(['e:\temp\', str, '.txt'], temp, 'precision', 10);
现在的问题是如何在文本文件中的每个第一维(我们有240个这个维度)之后添加\r\n\r\n
字符串(两个输入按下的键)?我该怎么办?毕竟我想拥有这种格式:
0.324235,...(20*10428 numbers),0.4363423,\r\n\r\n,
0.5467354,...(20*10428 numbers),0.346564,...
注意:此数组来自.nc
个文件,我想用这种方式将它们转换为.txt
文件
答案 0 :(得分:2)
在可定制性方面,MATLAB附带的方便花哨的套装实际上非常有限。每当你使用自定义格式编写一些文件时,它就会派上用场,以便知道如何自己编写:
% Open file for writing, safely
fid = fopen(fullfile('e:\temp\', str, '.txt'), 'w');
OC = onCleanup(@() any(fopen('all')==fid) && fclose(fid));
% Simply loop through all rows
for ii = 1:size(temp,1)
% Format the numbers, with comma as separator
line = sprintf('%.10f,', temp(ii,:)); % (trick to concatenate last dimension into second one)
line(end) = []; %(remove last comma)
% Print this line, adding two PC-type newlines
fprintf(fid, '%s\r\n\r\n', line);
end
% Clean up
fclose(fid);