comparison scripts/general/num2str.m @ 4914:1c0442da75fd

[project @ 2004-07-23 16:55:13 by jwe]
author jwe
date Fri, 23 Jul 2004 16:55:13 +0000
parents 14027e0bafa4
children d2a7208f35d4
comparison
equal deleted inserted replaced
4913:c8368716bab3 4914:1c0442da75fd
48 endif 48 endif
49 else 49 else
50 ## Setup a suitable format string 50 ## Setup a suitable format string
51 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) 51 if (isnumeric (x) && round (x) == x && abs (x) < 1e10)
52 dgt1 = ceil (log10 (max (max (abs (real (x(:)))), 52 dgt1 = ceil (log10 (max (max (abs (real (x(:)))),
53 max (abs (imag (x(:))))))) + 1; 53 max (abs (imag (x(:))))))) + 1;
54 dgt2 = dgt1 - (min (real (x(:))) >= 0); 54 dgt2 = dgt1 - (min (real (x(:))) >= 0);
55 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); 55 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1);
56 elseif (isscalar (x)) 56 elseif (isscalar (x))
57 fmt = "%.4g%-+.4gi"; 57 fmt = "%.4g%-+.4gi";
58 else 58 else
59 fmt = "%11.4g%-+11.4gi"; 59 fmt = "%11.4g%-+11.4gi";
60 endif 60 endif
61 endif 61 endif
62 62
63 ## Manipulate the complex value to have real values in the odd 63 ## Manipulate the complex value to have real values in the odd
64 ## columns and imaginary values in the even columns 64 ## columns and imaginary values in the even columns.
65 sz = size (x); 65 sz = size (x);
66 nc = sz (2); 66 nc = sz(2);
67 nd = ndims (x); 67 nd = ndims (x);
68 perm = fix ([1 : 0.5 : nc + 0.5]); 68 perm = fix ([1:0.5:nc+0.5]);
69 perm (2 : 2 : 2*nc) = perm(2 : 2 : 2*nc) + nc; 69 perm(2:2:2*nc) = perm(2:2:2*nc) + nc;
70 idx = cell (); 70 idx = cell ();
71 for i = 1:nd 71 for i = 1:nd
72 idx {i} = 1:sz(i); 72 idx {i} = 1:sz(i);
73 endfor 73 endfor
74 idx {2} = perm; 74 idx{2} = perm;
75 x = horzcat (real(x), imag(x)); 75 x = horzcat (real(x), imag(x));
76 x = x (idx{:}); 76 x = x(idx{:});
77 77
78 fmt = strcat (deblank(repmat (fmt, 1, nc)), "\n"); 78 fmt = strcat (deblank (repmat (fmt, 1, nc)), "\n");
79 tmp = sprintf (fmt, permute (x, [2, 1, 3 : nd])); 79 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd]));
80 80
81 ## Put the "i"'s where they are supposed to be. 81 ## Put the "i"'s where they are supposed to be.
82 while (true) 82 while (true)
83 tmp2 = strrep (tmp, " i\n", "i\n"); 83 tmp2 = strrep (tmp, " i\n", "i\n");
84 if (length(tmp) == length(tmp2)) 84 if (length (tmp) == length (tmp2))
85 break; 85 break;
86 else 86 else
87 tmp = tmp2; 87 tmp = tmp2;
88 endif 88 endif
89 endwhile 89 endwhile
105 else 105 else
106 fmt = sprintf ("%%.%dg", arg); 106 fmt = sprintf ("%%.%dg", arg);
107 endif 107 endif
108 else 108 else
109 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) 109 if (isnumeric (x) && round (x) == x && abs (x) < 1e10)
110 if (max(abs(x(:))) == 0) 110 if (max (abs (x(:))) == 0)
111 dgt = 1; 111 dgt = 1;
112 else 112 else
113 dgt = floor(log10(max(abs(x(:))))) + (min (real (x(:))) < 0) + 1; 113 dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 1;
114 endif 114 endif
115 fmt = sprintf("%%%dd ",dgt); 115 fmt = sprintf ("%%%dd ", dgt);
116 elseif (isscalar (x)) 116 elseif (isscalar (x))
117 fmt = "%.4g"; 117 fmt = "%.4g";
118 else 118 else
119 fmt = "%11.4g"; 119 fmt = "%11.4g";
120 endif 120 endif
121 endif 121 endif
122 fmt = strcat (deblank (repmat (fmt, 1, columns (x))), "\n"); 122 fmt = strcat (deblank (repmat (fmt, 1, columns (x))), "\n");
123 tmp = sprintf (fmt, permute (x, [2, 1, 3 : ndims(x)])); 123 nd = ndims (x);
124 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd]));
124 tmp(length (tmp)) = ""; 125 tmp(length (tmp)) = "";
125 retval = split (tmp, "\n"); 126 retval = split (tmp, "\n");
126 endif 127 endif
127 128
128 endfunction 129 endfunction