print cells type in Matlab -


i have cell array like:

>>text  'sentence1' 'sentence2' 'sentence3' 

whenever use

sprintf(fid,'%s\n',text) 

i error saying:

'function not defined 'cell' inputs.' 

but if put :

sprintf(fid,'%s\n',char(text)) 

it works in file appears sentences mixed no sense.

can recommend me do?

whener put text get:

>>text 'title ' 'author' 'comments ' {3x1} cell 

that why can not use text{:}.

if issue

sprintf('%s\n', text) 

you saying "print string newline. string cell array". that's not correct; cell-array not string.

if issue

sprintf('%s\n', char(text)) 

you saying "print string newline. string cell array, convert character array.". thing is, conversion results in single character array, , sprintf re-use %s\n format multiple inputs. moreover, writes single character array by column, meaning, characters in first column, concatenated horizontally characters second column, concatenated characters third column, etc.

therefore, approprate call sprintf multiple inputs:

sprintf(fid, '%s\n', text{:}) 

because cell-expansion text{:} creates comma-separated list entries in cell-array, sprintf/fprintf expects.

edit indicate:, have non-char entries in text. have check that. if want pass strings fprintf, use

fprintf(fid, '%s\n', text{ cellfun('isclass', text, 'char') }) 

if {3x1 cell} again set of strings, want write strings recursively, use this:

function textwriter       text = {...         'title'         'author'         'comments'         {...             'title2'             'author2'             'comments2'             {...                 'title3'                 'author3'                 'comments3'             }         }     }      text = cell2str(text);     fprintf(fid, '%s\n', text{:});   end   function out = cell2str(c)      out = {};     ii = c(:)'         if iscell(ii{1})             s = cell2str(ii{1});             out(end+1:end+numel(s)) = s;         else             out{end+1} = ii{1};         end     end  end 

Comments