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:
fprintf to cell array

Subject: fprintf to cell array

From: Paul

Date: 4 Jul, 2013 23:10:21

Message: 1 of 9

Hello,
I am using this command:

fprintf('%3.2f %s ',B(1,1),'±',sdotm(1,1))

to output:

-0.02 ± 0.08

I need to assign this output to an element of a cell array. How can this be done?

Thanks!

Subject: fprintf to cell array

From: Nasser M. Abbasi

Date: 4 Jul, 2013 23:40:33

Message: 2 of 9

On 7/4/2013 6:10 PM, Paul wrote:
> Hello,
> I am using this command:
>
> fprintf('%3.2f %s ',B(1,1),'±',sdotm(1,1))
>
> to output:
>
> -0.02 ± 0.08
>
> I need to assign this output to an element of a cell array. How can this be done?
>
> Thanks!
>

put the result in a variable. Then put this variable in the cell
array. Use sprintf

EDU>> s=sprintf('%3.2f %s ',-0.02,'±',0.08)

s =

-0.02 ± 0.08

Subject: fprintf to cell array

From: dpb

Date: 5 Jul, 2013 04:31:02

Message: 3 of 9

On 7/4/2013 6:40 PM, Nasser M. Abbasi wrote:
...

> put the result in a variable. Then put this variable in the cell
> array. Use sprintf
>
> EDU>> s=sprintf('%3.2f %s ',-0.02,'±',0.08)
>
> s =
>
> -0.02 ± 0.08

No need for the intermediary; just enclose the expression in curly braces...

cell_array(idx)={sprintf('%3.2f %s ',-0.02,'±',0.08)};

--

Subject: fprintf to cell array

From: TideMan

Date: 5 Jul, 2013 04:57:18

Message: 4 of 9

On Friday, July 5, 2013 4:31:02 PM UTC+12, dpb wrote:
> On 7/4/2013 6:40 PM, Nasser M. Abbasi wrote:
>
> ...
>
>
>
> > put the result in a variable. Then put this variable in the cell
>
> > array. Use sprintf
>
> >
>
> > EDU>> s=sprintf('%3.2f %s ',-0.02,'',0.08)
>
> >
>
> > s =
>
> >
>
> > -0.02 0.08
>
>
>
> No need for the intermediary; just enclose the expression in curly braces...
>
>
>
> cell_array(idx)={sprintf('%3.2f %s ',-0.02,'',0.08)};
>
>
>
> --

And if you can't figure out how to type '', you can replace it with char(177).

Subject: fprintf to cell array

From: Paul

Date: 5 Jul, 2013 21:24:07

Message: 5 of 9

dpb <none@non.net> wrote in message <kr5i64$ceb$1@speranza.aioe.org>...
> On 7/4/2013 6:40 PM, Nasser M. Abbasi wrote:
> ...
>
> > put the result in a variable. Then put this variable in the cell
> > array. Use sprintf
> >
> > EDU>> s=sprintf('%3.2f %s ',-0.02,'±',0.08)
> >
> > s =
> >
> > -0.02 ± 0.08
>
> No need for the intermediary; just enclose the expression in curly braces...
>
> cell_array(idx)={sprintf('%3.2f %s ',-0.02,'±',0.08)};
>
> --

Thanks for the replies, but both proposed solutions entail manually inputting "-0.02 ± 0.08" in order to assign it to a cell array element. How can I essentially redirect the output of

fprintf('%3.2f %s ',B(1,1),'±',sdotm(1,1))

into a cell array element assignment?

Thanks again!

Subject: fprintf to cell array

From: dpb

Date: 5 Jul, 2013 21:35:16

Message: 6 of 9

On 7/5/2013 4:24 PM, Paul wrote:
...

> Thanks for the replies, but both proposed solutions entail manually
> inputting "-0.02 ± 0.08" in order to assign it to a cell array element.
> How can I essentially redirect the output of
>
> fprintf('%3.2f %s ',B(1,1),'±',sdotm(1,1))
>
> into a cell array element assignment?

As said, use sprintf instead of fprintf and encapsulate in curlies to
make it a cell...

You do, of course, still have to have gotten B and sdotm from somewhere
a priori...

 >> B(1)=-0.02;sdotm(1)=0.08;
 >> c(1)={sprintf('%3.2f %s ',B(1,1),'±',sdotm(1,1))}
c =
     '-0.02 ± 0.08 '
 >>

QED

--

Subject: fprintf to cell array

From: dpb

Date: 5 Jul, 2013 21:45:47

Message: 7 of 9

On 7/5/2013 4:35 PM, dpb wrote:
...

> >> B(1)=-0.02;sdotm(1)=0.08;
> >> c(1)={sprintf('%3.2f %s ',B(1,1),'±',sdotm(1,1))}
> c =
> '-0.02 ± 0.08 '
> >>
>

BTW, the field width '3' in the formatting string isn't wide enough to
be effective if you're going to try to line a bunch of these up or
create a consistent-length string. You would need that to be 5.2f to
have room for the sign, one leading digit, the decimal point and two
trailing digits. That, of course, only handles values <|10|; larger
magnitudes will need correspondingly more places.

If you're satisfied w/ the minimum width rather than fixed, then just
write '%.2f' which is, in essence, what the C i/o runtime has done
anyway as it ignores such inconsistencies.

--

Subject: fprintf to cell array

From: dpb

Date: 5 Jul, 2013 22:20:14

Message: 8 of 9

On 7/5/2013 4:45 PM, dpb wrote:
...

>> >> c(1)={sprintf('%3.2f %s ',B(1,1),'±',sdotm(1,1))}
>> c =
>> '-0.02 ± 0.08 '

...

> If you're satisfied w/ the minimum width rather than fixed, then just
> write '%.2f' which is, in essence, what the C i/o runtime has done
> anyway as it ignores such inconsistencies.

Oh, one last nit on the format string -- NB that there's an extra
trailing blank owing to the blank between the 'f' and '%s'. If this
doesn't bother, it's fine. If that'll cause grief later on, then write
the the explicit format string

'%.2f %s %.2f %s'

Again, salt to suit...

--

Subject: fprintf to cell array

From: dpb

Date: 5 Jul, 2013 22:33:54

Message: 9 of 9

On 7/5/2013 5:20 PM, dpb wrote:
> On 7/5/2013 4:45 PM, dpb wrote:
> ...
>
>>> >> c(1)={sprintf('%3.2f %s ',B(1,1),'±',sdotm(1,1))}
>>> c =
>>> '-0.02 ± 0.08 '
>
> ...
...

> Oh, one last nit on the format string -- NB that there's an extra
> trailing blank owing to the blank between the 'f' and '%s'. If this
> doesn't bother, it's fine. If that'll cause grief later on, then write
> the the explicit format string
>
> '%.2f %s %.2f %s'

Man, I can't type anything w/ a correction any more... :(

It's the space after the "%s" before the last "'" that's the culprit in
the trailing space, just to be precise.

--

Tags for this Thread

No tags are associated with 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