【问题标题】:Writing a cell (matlab) to a CSV file将单元格(matlab)写入 CSV 文件
【发布时间】:2013-01-11 03:34:48
【问题描述】:

如何保存此单元格

    data = cell([3 2]);
    data{1,1} = 'Bla';
    data{2,1} = 'Bla1';
    data{3,1} = 'Bla2';
    data{1,2} = '2';
    data{2,2} = '3';
    data{3,2} = '1'

到 csv 文件?

我试过了

dlmwrite('C:\Users\Ro\Desktop\Mestrado\Resultados\Tabelas\VaR_tab.csv',data,'delimiter',';')

但它给出了这个错误信息:

Error using dlmwrite (line 118)
The input cell array cannot be converted to a matrix.

【问题讨论】:

    标签: matlab csv cell


    【解决方案1】:

    我刚试过,效果很好:

    filename = 'test';
    cell2csv(filename,data)
    

    cell2csv 可用作

    function cell2csv(filename,cellArray,delimiter)
    % Writes cell array content into a *.csv file.
    % 
    % CELL2CSV(filename,cellArray,delimiter)
    %
    % filename      = Name of the file to save. [ i.e. 'text.csv' ]
    % cellarray    = Name of the Cell Array where the data is in
    % delimiter = seperating sign, normally:',' (default)
    %
    % by Sylvain Fiedler, KA, 2004
    % modified by Rob Kohr, Rutgers, 2005 - changed to english and fixed delimiter
    if nargin<3
        delimiter = ',';
    end
    
    datei = fopen(filename,'w');
    for z=1:size(cellArray,1)
        for s=1:size(cellArray,2)
    
            var = eval(['cellArray{z,s}']);
    
            if size(var,1) == 0
                var = '';
            end
    
            if isnumeric(var) == 1
                var = num2str(var);
            end
    
            fprintf(datei,var);
    
            if s ~= size(cellArray,2)
                fprintf(datei,[delimiter]);
            end
        end
        fprintf(datei,'\n');
    end
    fclose(datei);
    

    我复制了代码,因为我不知道该函数是否在 MathWorks 文件交换上可用。但谷歌也应该提供帮助。

    【讨论】:

    • Matlab R2015a 不支持
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2021-07-18
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    相关资源
    最近更新 更多