num2str works differently in 2.1.42,2.1.43

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

num2str works differently in 2.1.42,2.1.43

Andy Adler
I don't know whether this is a bug or intentional.
num2str now puts leading spaces.

I have some code that does:
  eval(['fname=p',num2str(idx),';']);
This no longer works.

$ /usr/bin/octave-2.1.36.exe -q
octave-2.1.36:1> ['fname=p',num2str(1),';']
ans = fname=p1;

$ /usr/local/oct2143-test/bin/octave-2.1.43.exe -q
octave-2.1.43:1> ['fname=p',num2str(1),';']
ans = fname=p          1;


Andy



Reply | Threaded
Open this post in threaded view
|

num2str works differently in 2.1.42,2.1.43

John W. Eaton-6
On 10-Jan-2003, Andy Adler <[hidden email]> wrote:

| I don't know whether this is a bug or intentional.
|
| num2str now puts leading spaces.
|
| I have some code that does:
|   eval(['fname=p',num2str(idx),';']);
| This no longer works.
|
| $ /usr/bin/octave-2.1.36.exe -q
| octave-2.1.36:1> ['fname=p',num2str(1),';']
| ans = fname=p1;
|
| $ /usr/local/oct2143-test/bin/octave-2.1.43.exe -q
| octave-2.1.43:1> ['fname=p',num2str(1),';']
| ans = fname=p          1;

It was somewhat intentional, since the Matlab docs claim that the
default format for num2str is 11.4g.  But that is apparenly not what
is used for scalar values.  Please try the following patch.

Thanks,

jwe


2003-01-10  John W. Eaton  <[hidden email]>

        * general/num2str.m: Don't specify field width for scalars.


Index: general/num2str.m
===================================================================
RCS file: /usr/local/cvsroot/octave/scripts/general/num2str.m,v
retrieving revision 1.15
diff -u -r1.15 num2str.m
--- general/num2str.m 18 Dec 2002 20:37:32 -0000 1.15
+++ general/num2str.m 11 Jan 2003 04:21:38 -0000
@@ -39,7 +39,11 @@
  fmt = sprintf ("%%.%dg", arg);
       endif
     else
-      fmt = "%11.4g";
+      if (isscalar (x))
+ fmt = "%.4g";
+      else
+ fmt = "%11.4g";
+      endif
     endif
     if (iscomplex (x))
       error ("num2str: sorry, can't handle complex numbers yet");