Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to print Cell with text and numbers?

Subject: How to print Cell with text and numbers?

From: Stan

Date: 27 Aug, 2012 17:17:29

Message: 1 of 5

Hi,

I need to print an 11 X 24 cell array (named STA). It has text and number in it.

I am having difficulty with

>> fprintf(fid1,fmt, STA')

It gives an error message that it does not recognize a reference to a cell.

Is there a way to print a cell array in MATLAB, if that array contains text and numbers?

Subject: How to print Cell with text and numbers?

From: dpb

Date: 27 Aug, 2012 18:37:46

Message: 2 of 5

On 8/27/2012 12:17 PM, Stan wrote:
> Hi,
>
> I need to print an 11 X 24 cell array (named STA). It has text and
> number in it.
>
> I am having difficulty with
>
>>> fprintf(fid1,fmt, STA')
>
> It gives an error message that it does not recognize a reference to a cell.
>
> Is there a way to print a cell array in MATLAB, if that array contains
> text and numbers?

Dereference the cell string contents via char()...

--

Subject: How to print Cell with text and numbers?

From: reza

Date: 27 Aug, 2012 19:09:30

Message: 3 of 5

On Aug 27, 1:17pm, "Stan " <studentt...@hotmail.com> wrote:
> Hi,
>
> I need to print an 11 X 24 cell array (named STA). It has text and number in it.
>
> I am having difficulty with
>
> >> fprintf(fid1,fmt, STA')
>
> It gives an error message that it does not recognize a reference to a cell.
>
> Is there a way to print a cell array in MATLAB, if that array contains text and numbers?

Here is a cheap way that will need improving. It uses ',' as the
delimiter

joinstr = @(x,y) sprintf(sprintf('%%s%s', y), x{:});
lines = arrayfun(@(x) joinstr(STA(x,:), ','), 1:size(STA,1),
'UniformOutput', false);
fprintf(fid, '%s', joinstr(lines, '\n'));

note the trailing delimiters at the end of the line. I'll leave it to
you to write the code to have them removed.

/reza

Subject: How to print Cell with text and numbers?

From: reza

Date: 27 Aug, 2012 19:11:43

Message: 4 of 5

On Aug 27, 3:09pm, reza <mjahan...@gmail.com> wrote:
> On Aug 27, 1:17pm, "Stan " <studentt...@hotmail.com> wrote:
>
> > Hi,
>
> > I need to print an 11 X 24 cell array (named STA). It has text and number in it.
>
> > I am having difficulty with
>
> > >> fprintf(fid1,fmt, STA')
>
> > It gives an error message that it does not recognize a reference to a cell.
>
> > Is there a way to print a cell array in MATLAB, if that array contains text and numbers?
>
> Here is a cheap way that will need improving. It uses ',' as the
> delimiter
>
> joinstr = @(x,y) sprintf(sprintf('%%s%s', y), x{:});
> lines = arrayfun(@(x) joinstr(STA(x,:), ','), 1:size(STA,1),
> 'UniformOutput', false);
> fprintf(fid, '%s', joinstr(lines, '\n'));
>
> note the trailing delimiters at the end of the line. I'll leave it to
> you to write the code to have them removed.
>
> /reza

Alternatively use cellwrite() from MFE,
http://www.mathworks.com/matlabcentral/fileexchange/7363-cellwrite

/reza

Subject: How to print Cell with text and numbers?

From: Stan

Date: 29 Aug, 2012 04:44:08

Message: 5 of 5

reza <mjahanbin@gmail.com> wrote in message <81b9794b-41ad-4cf6-8a5f-2a2d0e18840c@kr6g2000pbb.googlegroups.com>...
> On Aug 27, 3:09pm, reza <mjahan...@gmail.com> wrote:
> > On Aug 27, 1:17pm, "Stan " <studentt...@hotmail.com> wrote:
> >
> > > Hi,
> >
> > > I need to print an 11 X 24 cell array (named STA). It has text and number in it.
> >
> > > I am having difficulty with
> >
> > > >> fprintf(fid1,fmt, STA')
> >
> > > It gives an error message that it does not recognize a reference to a cell.
> >
> > > Is there a way to print a cell array in MATLAB, if that array contains text and numbers?
> >
> > Here is a cheap way that will need improving. It uses ',' as the
> > delimiter
> >
> > joinstr = @(x,y) sprintf(sprintf('%%s%s', y), x{:});
> > lines = arrayfun(@(x) joinstr(STA(x,:), ','), 1:size(STA,1),
> > 'UniformOutput', false);
> > fprintf(fid, '%s', joinstr(lines, '\n'));
> >
> > note the trailing delimiters at the end of the line. I'll leave it to
> > you to write the code to have them removed.
> >
> > /reza
>
> Alternatively use cellwrite() from MFE,
> http://www.mathworks.com/matlabcentral/fileexchange/7363-cellwrite
>
> /reza

Here is some code and a bit more information:

%fmt to print matrix to a text file
>> fmt2=['%8d' ' %d/%02d/%02d %02d:%02d:%02d' '%10d' repmat('%22.10f',1,num_ret_parms) '\n'];

%print the matrix
>> fprintf(sav_fid2,fmt2,[file_counter DatTim(:,:) file_counter AAA(:,1:end-5)]');

%information about the matrix
>> size(AAA,2) = 29; %this means that AAA(:,1:end-5) has 24 columns

Next, I would like to print the cell array STA, so that its columns are aligned with AAA, like this:
>> fprintf( sav_fid2, fmt2_STA, STA' );
----------------X--------------

I cannot use cellwrite, etc. (I have searched and found a few others on MATLAB Exchange) since I need to sequentially write the cell array to a file:
a. where some other results (AAA, in the above code) is being written
b. in a particular format, so fprintf() is unfortunately my only option

Regarding the cell array STA:
It has 11 rows and 24 columns.i.e.

>> STA = cell(11,24);

Some cell entries are:
i. text
ii. numbers
iii. empty

Question:
1. What would be the fmt() command that I would need to use in order to fprintf the cell array?

Tags for this Thread

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.

Contact us