将单元格(matlab)写入CSV文件 [英] Writing a cell (matlab) to a CSV file

查看:3758
本文介绍了将单元格(matlab)写入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何保存此单元格

    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.


推荐答案

p>

I just tried this, and it worked:

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 File Exchange上是否还有这个函数。但Google也应该提供帮助。

I copied the code cause I am not aware if the function is any longer available on MathWorks File Exchange. But Google should help too.

这篇关于将单元格(matlab)写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆