Outputing cell array to CSV file ( MATLAB )

前端 未结 3 943
自闭症患者
自闭症患者 2021-01-06 18:34

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, whe

相关标签:
3条回答
  • 2021-01-06 19:14

    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 讨论(0)
  • 2021-01-06 19:21

    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
    
    0 讨论(0)
  • 2021-01-06 19:29

    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
    
    0 讨论(0)
提交回复
热议问题