I'm scratching my head reading through the help guide and previous newsgroup
posts and I can't figure out how to simply write a cell array of strings to a tab-
separated text file.
I have a cell array of N by M dimensions. Each element is a string-- numerical
data were converted via num2cell() and concatenated to other cell strings.
I want to write a text file with each row of the cell array on its own line with the
elements separated by tabs.
dlmwrite doesn't work because it doesn't recognize cell arrays.
Using a for loop with fprintf pops out with warnings that the %s format specifier
only works with type char.
"Jeff " <jkear_REMOVE_ns@ucsd.edu> wrote in message
<fc43dp$chq$1@fred.mathworks.com>...
> I'm scratching my head reading through the help guide and
previous newsgroup
> posts and I can't figure out how to simply write a cell
array of strings to a tab-
> separated text file.
>
> I have a cell array of N by M dimensions. Each element is
a string-- numerical
> data were converted via num2cell() and concatenated to
other cell strings.
>
> I want to write a text file with each row of the cell
array on its own line with the
> elements separated by tabs.
>
> dlmwrite doesn't work because it doesn't recognize cell
arrays.
>
> Using a for loop with fprintf pops out with warnings that
the %s format specifier
> only works with type char.
>
> Any ideas?
I cannot see why fprintf does not work. I created a 2x3 cell
with string elements and then I wrote:
.....
>> [M,N] = size(strs);
>> for (i=1:M)
>> for (j=1:N)
>> fprintf(fp, '%s\t',strs{i,j});
>> end
>> fprintf(fp, '\n');
>> end
.....
and everything worked fine.
Regards,
Theodore
"Jeff " <jkear_REMOVE_ns@ucsd.edu> wrote in message
<fc43dp$chq$1@fred.mathworks.com>...
> I have a cell array of N by M dimensions. Each element is
a string-- numerical
> data were converted via num2cell() and concatenated to
other cell strings.
> I want to write a text file with each row of the cell
array on its own line with the
> elements separated by tabs.
a = {'hey' 'there' 'what' 'is' 'up?'};
sprintf('%s\t', a{:})
You can generalize this to a 2D cell array by looping
over the row index.
> dlmwrite doesn't work because it doesn't recognize cell
arrays...
one of the solutions
-note: best look at it in the <original version> to observe
propper formatting...
% the data
c={
'a' 'bb' 'ccc' 'dddd'
'a1' 'b2' 'c3' 'd4'
'a a' 'b b' 'c c' 'd d'
};
fnam='foo.txt'; % <- your file name!
% the engine
c=c.';
txt=sprintf([repmat('%s\t',1,size(c,1)),'\n'],c{:});
dlmwrite(fnam,txt,'');
% the result
disp(txt)
type(fnam);
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central.
Read the complete Disclaimer prior to use.