在csv文件MATLAB

时间:2017-02-01 11:42:47

标签: matlab csv matrix save

我要附加到csv文件。当前代码输出一行:a; b; c; d 我需要输出a; b; c; d; 注意额外的';'在d结尾。这是至关重要的

 matrix = [a,b,c,d]
 dlmwrite('matrix.csv', matrix, 'delimiter',';','-append','roffset',0, 'precision',14)

任何帮助将不胜感激。 我必须将变量a,b,c和d保存为数字,或者使它成为一个字符向量(或其他东西),这使得我的csv看起来很有趣

1 个答案:

答案 0 :(得分:1)

我总是遇到MatLab inbuild CSV写入方法的问题。为什么不编写自己的.CSV编写方法?

在这里,你可以创建一个类似的函数:

function write_to_csv(filepath, matrix)
  csv = fopen(filepath, 'a+'); % check what sort of open you'd like : https://uk.mathworks.com/help/matlab/ref/fopen.html#inputarg_permission
  for ii = 1 : numel(matrix)   % this loop depends on the dimensions of your matrix
    fprintf(csv, '%s;', matrix(ii)); % check fprintf return type, depending on the data in the matrix : https://uk.mathworks.com/help/matlab/ref/fprintf.html?searchHighlight=fprintf&s_tid=doc_srchtitle#inputarg_formatSpec
  end 
  fclose(csv);
end

这适用于您提供的1D矩阵,以:

运行
write_to_csv('matrix.csv',matrix)