Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Printing a cell array

Subject: Printing a cell array

From: n o s p a m p l e a s e

Date: 09 May, 2008 06:16:03

Message: 1 of 6

I have a cell array as follows:

Names = {'Jon' ; 'Hur' ; 'Osl' ; 'Got'};

I want to print them such one element goes in a line as follows:

1 Jon
2 Hur
3 Osl
4 Got

I wrote the following code:
nb = 4;
fid = fopen('myoutput.txt','w');
fprintf(fid, '\n %d %s', [1:nb], Names{:});
fclose(fid);

but it is not working out properly.

Any suggestions.

NSP

Subject: Re: Printing a cell array

From: Bob

Date: 12 May, 2008 22:16:54

Message: 2 of 6

Does this help?

for k=1:nb
   fprintf('%d %s\n',k,Names{k})
end

Cheers
Bob

Subject: Re: Printing a cell array

From: n o s p a m p l e a s e

Date: 16 May, 2008 08:27:41

Message: 3 of 6

On May 13, 12:16=A0am, Bob <rbe...@mathworks.com> wrote:
> Does this help?
>
> for k=3D1:nb
> =A0 =A0fprintf('%d =A0 =A0%s\n',k,Names{k})
> end
>
> Cheers
> Bob

Yes it works but I want it in vector form i.e. something as follows:

fprintf('%d %s\n',[1:nb],Names{[1:nb]})

and the above doesn't work. Any guess how I can put it in vector form.

Thanx/NSP

Subject: Re: Printing a cell array

From: Jos

Date: 16 May, 2008 09:47:10

Message: 4 of 6

n o s p a m p l e a s e <nospam.please@alum.com> wrote in
message
<c796a606-58d8-4874-b4cc-d5c6d8602495@c65g2000hsa.googlegroups.com>...
> On May 13, 12:16=A0am, Bob <rbe...@mathworks.com> wrote:
> > Does this help?
> >
> > for k=3D1:nb
> > =A0 =A0fprintf('%d =A0 =A0%s\n',k,Names{k})
> > end
> >
> > Cheers
> > Bob
>
> Yes it works but I want it in vector form i.e. something
as follows:
>
> fprintf('%d %s\n',[1:nb],Names{[1:nb]})
>
> and the above doesn't work. Any guess how I can put it in
vector form.
>
> Thanx/NSP

An example of comma-separated list expansion:

x = 1:3 ;
t = {'AA','B','CCC'} ;

% one big cell array
M = [num2cell(x) ; t]

% expansion
fprintf('\n%d\t %s',M{:})

Note that the order of M is important, before you apply
comma-separated list expansion. You might have to transpose
M before.

hth
Jos

Subject: Re: Printing a cell array

From: Jos

Date: 16 May, 2008 09:47:10

Message: 5 of 6

n o s p a m p l e a s e <nospam.please@alum.com> wrote in
message
<c796a606-58d8-4874-b4cc-d5c6d8602495@c65g2000hsa.googlegroups.com>...
> On May 13, 12:16=A0am, Bob <rbe...@mathworks.com> wrote:
> > Does this help?
> >
> > for k=3D1:nb
> > =A0 =A0fprintf('%d =A0 =A0%s\n',k,Names{k})
> > end
> >
> > Cheers
> > Bob
>
> Yes it works but I want it in vector form i.e. something
as follows:
>
> fprintf('%d %s\n',[1:nb],Names{[1:nb]})
>
> and the above doesn't work. Any guess how I can put it in
vector form.
>
> Thanx/NSP

An example of comma-separated list expansion:

x = 1:3 ;
t = {'AA','B','CCC'} ;

% one big cell array
M = [num2cell(x) ; t]

% expansion
fprintf('\n%d\t %s',M{:})

Note that the order of M is important, before you apply
comma-separated list expansion. You might have to transpose
M before.

hth
Jos

Subject: Re: Printing a cell array

From: Nitin Chhabra

Date: 16 May, 2008 09:50:18

Message: 6 of 6

n o s p a m p l e a s e <nospam.please@alum.com> wrote in
message <c796a606-58d8-4874-b4cc-
d5c6d8602495@c65g2000hsa.googlegroups.com>...
> On May 13, 12:16=A0am, Bob <rbe...@mathworks.com> wrote:
> > Does this help?
> >
> > for k=3D1:nb
> > =A0 =A0fprintf('%d =A0 =A0%s\n',k,Names{k})
> > end
> >
> > Cheers
> > Bob
>
> Yes it works but I want it in vector form i.e. something
as follows:
>
> fprintf('%d %s\n',[1:nb],Names{[1:nb]})
>
> and the above doesn't work. Any guess how I can put it
in vector form.
>
> Thanx/NSP

There is one more crude way.
Try this if it works :

fid = fopen('myoutput.txt','w');
index = { '1 ' ; '2 ' ; '3 '; '4 '};
Names = {'Jon' ; 'Hur' ; 'Osl' ; 'Got'};
Names2=(strcat(index',Names'))';
fprintf(fid, '\n %s',Names2{:});
fclose(fid);

cheers,
Nitin


Tags for this Thread

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

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.
Related Topics