开发者

Outputing cell array to CSV file ( MATLAB )

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


Last commment was written in "pure" C. So It doesnt work in Matlab.

Here it is the right solution.

function [ ] = writecellmatrixtocsvfile( filename, matrix )
%WRITECELLMATRIXTOCSVFILE Summary of this function goes here
%   Detailed explanation goes here
fid = fopen(filename,'w');
for i = 1:size(matrix,1)
    for j = 1:size(matrix,2)
        fprintf(fid,'%s',matrix{i,j});
        if j~=size(matrix,2)
            fprintf(fid,'%s',',');
        else
            fprintf(fid,'\n');
        end
    end
end
fclose(fid);
end


easy enough to write your own csv writer.

-- edited to reflect comments --

fid = fopen('myfilename.csv','w');
for i = 1:size(A,1)
   for j = 1:size(A,2)
      fprintf(fid,'%s',A{i,j});
      if(j!=size(A,2)
         fprintf(fid,',',A{i,j})
      else
         fprintf(fid,'\n')
      end
   end
end
fclose(fid);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜