Outputing cell array to CSV file ( MATLAB )

10,098

Solution 1

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

Solution 2

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
Share:
10,098
Bob M.
Author by

Bob M.

Updated on June 05, 2022

Comments

  • Bob M.
    Bob M. almost 2 years

    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 :)

  • Bob M.
    Bob M. almost 13 years
    Thanks for your reply, this worked great. I found my problem was my original cell array, I each cell in the cell array, was in fact a cell array itself containing 1 string ( ...I'm a fool!) No wonder fprintf had a fit when I was passing it a dodgy array!
  • Bob M.
    Bob M. almost 13 years
    I can see what you are trying to do, but I think sprintf should be fprintf. Thanks again BlessedKey!