2017-02-01 192 views
1

我附加到一个csv文件。当前代码输出一行:a; b; c; d 我需要它输出一个; b; c; d; 注意额外';'在d的末尾。这是必不可少的添加一个分隔符到csv文件的每一行的末尾MATLAB

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

任何帮助,将不胜感激。 我不得不保留变量a,b,c和d作为数字,或者它使它成为我的csv看起来很滑稽的字符向量(或其他东西)

+0

1.-你为什么需要在CSV看起来“很好”?你只是存储数据。 2.你为什么需要那个最后的';'?如果您再添加更多行,您很可能会破坏阅读代码。 –

+0

它运行到另一块软件中,它将每行读取为一个数组。如果每一行的结尾都没有“;”,那么软件就会崩溃。 – Lucas

+1

我的建议是:使用'fprintf',右边格式化 –

回答

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 

这适用于你提供一个一维矩阵,运行:

write_to_csv('matrix.csv',matrix) 
+0

嗨,好的回答!谢谢! 你能帮我看看这个问题吗? http://stackoverflow.com/questions/41981820/matlab-fprintf-precision-not-working – Lucas

相关问题