Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cellfun and num2str

13 views
Skip to first unread message

wapo

unread,
Aug 21, 2008, 8:44:35 AM8/21/08
to
Hi all,

In the following code:

a={[0.111 0.220545];
[0.0000001 0.00023]};
b=cellfun(@(x) num2str(x,3),a,'UniformOutput',false);

I would expect that num2str would limit the significant decimal
elements into 3. But thats not the case. It works for the first row
but not the second. Why?

Thanks in advance

wapo

unread,
Aug 21, 2008, 10:59:19 AM8/21/08
to
Follow up,

It looks like that the dysfunctional element in the above code is
solely num2str and not the cellfun function.

Titus

unread,
Aug 21, 2008, 11:48:51 AM8/21/08
to

"wapo" <apostolos...@gmail.com> schrieb im Newsbeitrag
news:e3335d6c-b864-4e62...@m3g2000hsc.googlegroups.com...

> Follow up,
>
> It looks like that the dysfunctional element in the above code is
> solely num2str and not the cellfun function.

hmm, as far as I can see the strings look fine (R2008a): I get

b =
'0.111 0.221'
'1e-007 0.00023'

and the second row shows (a maximum of) three significant digits (you gave
one and two sig. digits, and they are printed). Slightly different example
(but working as well)
a={[0.111 0.220545];
[0.0000001234 0.0002345]};
b=cellfun(@(x) num2str(x,3),a,'UniformOutput',false);

b =
'0.111 0.221'
'1.23e-007 0.000235'

Titus


Walter Roberson

unread,
Aug 21, 2008, 12:48:28 PM8/21/08
to

Looks about right to me. Recall that leading zeros in a number are
not considered to be "significant decimal places" unless you know
the potential range of values is such that it would have been possible
for the higher order digits to have been non-zero.

You may wish to use num2str(x,'%.2e ')
Notice the space after the e and before the terminal apostrophe.

--
Q = quotation(rand);
if isempty(Q); error('Quotation server filesystem problems')
else sprintf('%s',Q), end

dpb

unread,
Aug 21, 2008, 12:45:42 PM8/21/08
to

WAD(esigned)&D(ocumented)...

num2str()'s second argument controls max number of _significant_ digits

If otoh you want only n number of decimal digits, use sprintf() directly.

--

wapo

unread,
Aug 22, 2008, 3:11:17 AM8/22/08
to

Ok guys, sorry. Now I get it that it was my mistake.

0 new messages