我想在Matlab中将一些数据写入文本文件,但我遇到了问题。我希望文件看起来像这样
'AnatomicImage:13'
'AnatomicImageNumber:13'
'ColorMapMaximum:20'
'ColorMapMinimum:0'
'SpectroscopySliceNumber:3'
但是它会像这样写入文本文件
AnatomicImage:13,AnatomicImageNumber:13,ColorMapMaximum:20,ColorMapMinimum:0,SpectroscopySliceNumber:3
我已将我的代码段包含在Stackoverflow上的另一篇文章中。
AnatomicImage = 'AnatomicImage: ';
AnatomicImage = strcat(AnatomicImage, num2str(imNum));
AnatomicImageNumber = 'AnatomicImageNumber: '
AnatomicImageNumber = strcat(AnatomicImageNumber, num2str(imNum));
ColorMapMaximum = 'ColorMapMaximum: ';
ColorMapMaximum = strcat(ColorMapMaximum, num2str(max));
ColorMapMinimum = 'ColorMapMinimum: ';
ColorMapMinimum = strcat(ColorMapMinimum, num2str(min));
SpectroscopySliceNumber = 'SpectroscopySliceNumber: ';
SpectroscopySliceNumber = strcat(SpectroscopySliceNumber, num2str(sliceNum));
fid=fopen('data.txt','wt');
data = {AnatomicImage;AnatomicImageNumber;ColorMapMaximum; ColorMapMinimum; SpectroscopySliceNumber}
txtFun = @(str)sprintf('%s,',str)
xchar = cellfun(txtFun, data, 'UniformOutput', false)
xchar = strcat(xchar{:})
xchar = strcat(xchar(1:end-1),'\n')
fprintf(fid,xchar)
fclose(fid);
答案 0 :(得分:2)
将相关部分更改为:
txtFun = @(str)sprintf('%s,\n',str)
xchar = cellfun(txtFun, data, 'UniformOutput', false)
fid=fopen('data.txt','wt');
for i=1:numel(xchar)
fprintf(fid,'%s',xchar{i})
end
fclose(fid);
另外,请考虑使用安全文件 - 请参阅我的回答here