changeset 11485:571bfa4fc295

mat2str: handle logical arguments
author John W. Eaton <jwe@octave.org>
date Wed, 12 Jan 2011 01:15:23 -0500
parents cda6044447a0
children a1deab9a6e71
files scripts/ChangeLog scripts/strings/mat2str.m
diffstat 2 files changed, 30 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Jan 11 19:27:12 2011 +0100
+++ b/scripts/ChangeLog	Wed Jan 12 01:15:23 2011 -0500
@@ -1,3 +1,8 @@
+2011-01-12  John W. Eaton  <jwe@octave.org>
+
+	* strings/mat2str.m: Handle logical arguments.  New tests.
+	Bug #32102.
+
 2011-01-10  John W. Eaton  <jwe@octave.org>
 
 	* linear-algebra/expm.m: Validate nargin.  New tests.
--- a/scripts/strings/mat2str.m	Tue Jan 11 19:27:12 2011 +0100
+++ b/scripts/strings/mat2str.m	Wed Jan 12 01:15:23 2011 -0500
@@ -68,7 +68,7 @@
     endif
   endif
 
-  if (nargin < 1 || nargin > 3 || ! isnumeric (x))
+  if (nargin < 1 || nargin > 3 || ! (isnumeric (x) || islogical (x)))
     print_usage ();
   endif
   
@@ -76,15 +76,19 @@
     error ("mat2str: X must be two dimensional");
   endif
 
+  x_islogical = islogical (x);
   x_iscomplex = iscomplex (x);
 
-  if (! x_iscomplex)
-    fmt = sprintf ("%%.%dg", n(1));
-  else
+  if (x_iscomplex)
     if (length (n) == 1)
       n = [n, n];
     endif
     fmt = sprintf ("%%.%dg%%+.%dgi", n(1), n(2));
+  elseif (x_islogical)
+    v = {"false", "true"};
+    fmt = "%s";
+  else
+    fmt = sprintf ("%%.%dg", n(1));
   endif
 
   nel = numel (x);
@@ -94,22 +98,27 @@
     s = "[]";
   elseif (nel == 1)
     ## Scalar X, don't print brackets
-    if (! x_iscomplex)
+    if (x_iscomplex)
+      s = sprintf (fmt, real (x), imag (x));
+    elseif (x_islogical)
+      s = v{x+1};
+    else
       s = sprintf (fmt, x);
-    else
-      s = sprintf (fmt, real (x), imag (x));
     endif
   else
     ## Non-scalar X, print brackets
-    fmt = [fmt, ","];
-    if (! x_iscomplex)
-      s = sprintf (fmt, x.');
-    else
+    fmt = cstrcat (fmt, ",");
+    if (x_iscomplex)
       t = x.';
       s = sprintf (fmt, [real(t(:))'; imag(t(:))']);
+    elseif (x_islogical)
+      t = v(x+1);
+      s = cstrcat (sprintf (fmt, t{:}));
+    else
+      s = sprintf (fmt, x.');
     endif
 
-    s = ["[", s];
+    s = cstrcat ("[", s);
     s(end) = "]";
     ind = find (s == ",");
     nc = columns (x);
@@ -117,7 +126,7 @@
   endif
 
   if (strcmp ("class", cls))
-    s = [class(x), "(", s, ")"];
+    s = cstrcat (class(x), "(", s, ")");
   endif
 endfunction
 
@@ -125,3 +134,6 @@
 %!assert (mat2str ([-1/3 +i/7; 1/3 -i/7], [4 2]), "[-0.3333+0i,0+0.14i;0.3333+0i,-0-0.14i]")
 %!assert (mat2str (int16 ([1 -1]), 'class'), "int16([1,-1])")
 
+%!assert (mat2str (true), "true");
+%!assert (mat2str (false), "false");
+%!assert (mat2str (logical (eye (2))), "[true,false;false,true]");