Thread Subject: write cell array of strings

Subject: write cell array of strings

From: Jeff

Date: 10 Sep, 2007 18:46:17

Message: 1 of 4

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?

Subject: write cell array of strings

From: Theodoros Giannakopoulos

Date: 10 Sep, 2007 19:17:50

Message: 2 of 4

"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

-----------------------------------
- http://www.di.uoa.gr/tyiannak -
-----------------------------------


Subject: write cell array of strings

From: Walter Roberson

Date: 10 Sep, 2007 19:29:48

Message: 3 of 4

"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.

Subject: write cell array of strings

From: us

Date: 10 Sep, 2007 20:13:24

Message: 4 of 4

Jeff:
<SNIP <dlmwrite>ing cells headache...

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

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
code us 10 Sep, 2007 16:15:28
dlmwrite us 10 Sep, 2007 16:15:28
write to file Jeff 10 Sep, 2007 14:50:26
cell array Jeff 10 Sep, 2007 14:50:26
rssFeed for this Thread

Public Submission Policy

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.

Contact us at files@mathworks.com