将单元格阵列输出为CSV文件(MATLAB) [英] Outputing cell array to CSV file ( MATLAB )

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

问题描述

我使用 cell(m,n)创建了amxn单元格数组,并用任意字符串填充每个单元格。



如何将单元格数组输出为CSV文件,其中数组中的每个单元格都是CSV电子表格中的单元格。



I我尝试使用 cell2CSV ,但我收到错误...

 错误==> cell2csv at 71 
fprintf(datei,'%s',var);

造成:

 使用==>时出错dlmwrite at 114 
输入单元格数组不能转换为矩阵。任何指导都会受到好评:)

h2_lin>解决方案

这里是一个有点矢量化的解决方案:

 %#允许创建一个cellarray填充与随机字符串
C = cell(10,5);
chars = char(97:122);
for i = 1:numel(C)
C {i} = chars(ceil(numel(chars)。rand(1,randi(10))));
end

%#构建行列的单元格,值以逗号分隔
[m n] = size(C);
CC = cell(m,n + n-1);
CC(:,1:2:end)= C;
CC(:,2:2:end,:) = {','};
CC = arrayfun(@(i)[CC {i,:}],1:m,'UniformOutput',false)'; %'

%#将行写入文件
fid = fopen('output.csv','wt');
fprintf(fid,'%s\\\
',CC {:});
fclose(fid);

字符串:

  C = 
'rdkhshx''egxpnpvnfl''qnwcxcndo''gubkafae''yvsejeaisq'
'kmsvpoils''zqssj''t''ge''lhntto'
'sarlldvig''oeoslv''xznhcnptc''px''qdnjcdfr'
'jook''jlkutlsy''neyplyr''fmjngbleay'sganh'
'nrys''sckplbfv''vnorj''ztars' 'xkarvzblpr'
'vdbce''w''pwk''ofufjxw''qsjpdbzh'
'haoc''r''lh''ipxxp''zefiyk'
'qw''fodrpb ''vkkjd''wlxa''dkj'
'ozonilmbxb''d''clg''seieik''lc'
'vkpvx''l''ldm''bohgge''aouglob'

生成的CSV文件



  rdkhshx,egxpnpvnfl,qnwcxcndo,gubkafae,yvsejeaisq 
kmsvpoils,zqssj,t,ge,lhntto
sarlldvig,oeoslv,xznhcnptc,px,qdnjcdfr
jook,jlkutlsy,neyplyr ,fmjngbleay,sganh
nrys,sckplbfv,vnorj,ztars,xkarvzblpr
vdbce,w,pwk,ofufjxw,qsjpdbzh
haoc,r,lh,ipxxp,zefiyk
qw,fodrpb ,vkkjd,wlxa,dkj
ozonilmbxb,d,clg,seieik,lc
vkpvx,l,ldm,bohgge,aouglob


I've created a m x n cell array using cell(m,n), and filled each of the cells with arbitrary strings.

How do I output the cell array as a CSV file, where each cell in the array is a cell in the CSV 'spreadsheet'.

I've tried using cell2CSV, but I get errors ...

Error in ==> cell2csv at 71
    fprintf(datei, '%s', var);

Caused by:

Error using ==> dlmwrite at 114
The input cell array cannot be converted to a matrix.

Any guidance will be well received :)

解决方案

Here is a somewhat vectorized solution:

%# lets create a cellarray filled with random strings
C = cell(10,5);
chars = char(97:122);
for i=1:numel(C)
    C{i} = chars(ceil(numel(chars).*rand(1,randi(10))));
end

%# build cellarray of lines, values are comma-separated
[m n] = size(C);
CC = cell(m,n+n-1);
CC(:,1:2:end) = C;
CC(:,2:2:end,:) = {','};
CC = arrayfun(@(i) [CC{i,:}], 1:m, 'UniformOutput',false)';     %'

%# write lines to file
fid = fopen('output.csv','wt');
fprintf(fid, '%s\n',CC{:});
fclose(fid);

The strings:

C = 
    'rdkhshx'       'egxpnpvnfl'    'qnwcxcndo'    'gubkafae'      'yvsejeaisq'
    'kmsvpoils'     'zqssj'         't'            'ge'            'lhntto'    
    'sarlldvig'     'oeoslv'        'xznhcnptc'    'px'            'qdnjcdfr'  
    'jook'          'jlkutlsy'      'neyplyr'      'fmjngbleay'    'sganh'     
    'nrys'          'sckplbfv'      'vnorj'        'ztars'         'xkarvzblpr'
    'vdbce'         'w'             'pwk'          'ofufjxw'       'qsjpdbzh'  
    'haoc'          'r'             'lh'           'ipxxp'         'zefiyk'    
    'qw'            'fodrpb'        'vkkjd'        'wlxa'          'dkj'       
    'ozonilmbxb'    'd'             'clg'          'seieik'        'lc'        
    'vkpvx'         'l'             'ldm'          'bohgge'        'aouglob'   

The resulting CSV file:

rdkhshx,egxpnpvnfl,qnwcxcndo,gubkafae,yvsejeaisq
kmsvpoils,zqssj,t,ge,lhntto
sarlldvig,oeoslv,xznhcnptc,px,qdnjcdfr
jook,jlkutlsy,neyplyr,fmjngbleay,sganh
nrys,sckplbfv,vnorj,ztars,xkarvzblpr
vdbce,w,pwk,ofufjxw,qsjpdbzh
haoc,r,lh,ipxxp,zefiyk
qw,fodrpb,vkkjd,wlxa,dkj
ozonilmbxb,d,clg,seieik,lc
vkpvx,l,ldm,bohgge,aouglob

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

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