diff scripts/general/num2str.m @ 4878:4eaf35cfdb11

[project @ 2004-04-23 15:42:10 by jwe]
author jwe
date Fri, 23 Apr 2004 15:42:10 +0000
parents bdc51b369a78
children 14027e0bafa4
line wrap: on
line diff
--- a/scripts/general/num2str.m
+++ b/scripts/general/num2str.m
@@ -31,7 +31,74 @@
 
 function retval = num2str (x, arg)
 
-  if (nargin == 1 || nargin == 2)
+  if (nargin != 1 && nargin != 2)
+    usage ("num2str (x) or num2str (x, precision) or num2str (x, fmt)");
+  endif
+
+  if (isstr (x))
+    retval = x;
+  endif
+
+  if (iscomplex (x))
+    if (nargin == 2)
+      if (isstr (arg))
+	fmt = strcat (arg, "%-+", arg(2:end), "i");
+      else
+	fmt = sprintf ("%%.%dg%%-+.%dgi", arg);
+      endif
+    else
+      ## Setup a suitable format string
+      if (isnumeric (x) && round (x) == x && abs (x) < 1e10)
+	dgt1 = ceil (log10 (max (max (abs (real (x(:)))),
+			     max (abs (imag (x(:))))))) + 1;
+	dgt2 = dgt1 - (min (real (x(:))) >= 0);
+	fmt = sprintf("%%%dd%%+-%ddi  ", dgt2, dgt1);
+      elseif (isscalar (x))
+	fmt = "%.4g%-+.4gi";
+      else
+	fmt = "%11.4g%-+11.4gi";
+      endif
+    endif
+
+    ## Manipulate the complex value to have real values in the odd
+    ## columns and imaginary values in the even columns
+    sz = size (x);
+    nc = sz (2);
+    nd = ndims (x);
+    perm = fix ([1 : 0.5 : nc + 0.5]);
+    perm (2 : 2 : 2*nc) = perm(2 : 2 : 2*nc) + nc;
+    idx = cell ();
+    for i = 1:nd
+      idx {i} = 1:sz(i);
+    endfor
+    idx {2} = perm;
+    x = horzcat (real(x), imag(x));
+    x = x (idx{:});
+    
+    fmt = strcat (deblank(repmat (fmt, 1, nc)), "\n");
+    tmp = sprintf (fmt, permute (x, [2, 1, 3 : nd]));
+
+    ## Put the "i"'s where they are supposed to be.
+    while (true)
+      tmp2 = strrep (tmp, " i\n", "i\n");
+      if (length(tmp) == length(tmp2))
+	break;
+      else
+	tmp = tmp2;
+      endif
+    endwhile
+    while (true)
+      tmp2 = strrep (tmp, " i", "i ");
+      if (tmp == tmp2)
+	break;
+      else
+	tmp = tmp2;
+      endif
+    endwhile
+
+    tmp(length (tmp)) = "";
+    retval = split (tmp, "\n");
+  else
     if (nargin == 2)
       if (isstr (arg))
 	fmt = arg;
@@ -40,25 +107,18 @@
       endif
     else
       if (isnumeric (x) && round (x) == x && abs (x) < 1e10)
-	fmt = "%d";
+	dgt = ceil(log10(max(abs(x(:)))))+ (min (real (x(:))) < 0);
+	fmt = sprintf("%%%dd  ",dgt);
       elseif (isscalar (x))
 	fmt = "%.4g";
       else
 	fmt = "%11.4g";
       endif
     endif
-    if (isstr (x))
-      retval = x;
-    elseif (iscomplex (x))
-      error ("num2str: sorry, can't handle complex numbers yet");
-    else
-      fmt = strcat (repmat (fmt, 1, columns (x)), "\n");
-      tmp = sprintf (fmt, x.');
-      tmp(length (tmp)) = "";
-      retval = split (tmp, "\n");
-    endif
-  else
-    usage ("num2str (x) or num2str (x, precision) or num2str (x, fmt)");
+    fmt = strcat (deblank (repmat (fmt, 1, columns (x))), "\n");
+    tmp = sprintf (fmt, permute (x, [2, 1, 3 : ndims(x)]));
+    tmp(length (tmp)) = "";
+    retval = split (tmp, "\n");
   endif
 
 endfunction