Mercurial > hg > octave-lyh
comparison scripts/general/num2str.m @ 6975:0d11a12643f1
[project @ 2007-10-08 18:23:44 by jwe]
author | jwe |
---|---|
date | Mon, 08 Oct 2007 18:23:44 +0000 |
parents | 69e864d21c11 |
children | 6d0d8d621532 |
comparison
equal
deleted
inserted
replaced
6974:9e32bb109980 | 6975:0d11a12643f1 |
---|---|
54 ## Setup a suitable format string | 54 ## Setup a suitable format string |
55 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) | 55 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) |
56 dgt1 = ceil (log10 (max (max (abs (real (x(:)))), | 56 dgt1 = ceil (log10 (max (max (abs (real (x(:)))), |
57 max (abs (imag (x(:))))))) + 1; | 57 max (abs (imag (x(:))))))) + 1; |
58 dgt2 = dgt1 - (min (real (x(:))) >= 0); | 58 dgt2 = dgt1 - (min (real (x(:))) >= 0); |
59 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); | 59 |
60 if (length (abs (x) == x) > 0) | |
61 fmt = sprintf("%%%dg%%+-%dgi ", dgt2, dgt1); | |
62 else | |
63 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); | |
64 endif | |
60 elseif (isscalar (x)) | 65 elseif (isscalar (x)) |
61 fmt = "%.4g%-+.4gi"; | 66 fmt = "%.6g%-+.6gi"; |
62 else | 67 else |
63 fmt = "%11.4g%-+11.4gi"; | 68 fmt = "%11.6g%-+11.6gi"; |
64 endif | 69 endif |
65 endif | 70 endif |
66 | 71 |
67 ## Manipulate the complex value to have real values in the odd | 72 ## Manipulate the complex value to have real values in the odd |
68 ## columns and imaginary values in the even columns. | 73 ## columns and imaginary values in the even columns. |
99 tmp = tmp2; | 104 tmp = tmp2; |
100 endif | 105 endif |
101 endwhile | 106 endwhile |
102 | 107 |
103 tmp(length (tmp)) = ""; | 108 tmp(length (tmp)) = ""; |
104 retval = split (tmp, "\n"); | 109 retval = strtrim (split (tmp, "\n")); |
105 else | 110 else |
106 if (nargin == 2) | 111 if (nargin == 2) |
107 if (ischar (arg)) | 112 if (ischar (arg)) |
108 fmt = arg; | 113 fmt = arg; |
109 else | 114 else |
118 if (max (abs (x(:))) == 0) | 123 if (max (abs (x(:))) == 0) |
119 dgt = 1; | 124 dgt = 1; |
120 else | 125 else |
121 dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 1; | 126 dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 1; |
122 endif | 127 endif |
123 fmt = sprintf ("%%%dd ", dgt); | 128 if (length (abs (x) == x) > 0) |
129 fmt = sprintf ("%%%dg ", dgt); | |
130 else | |
131 fmt = sprintf ("%%%dd ", dgt); | |
132 endif | |
124 elseif (isscalar (x)) | 133 elseif (isscalar (x)) |
125 fmt = "%.4g"; | 134 fmt = "%11.5g"; |
126 else | 135 else |
127 fmt = "%11.4g"; | 136 fmt = "%11.5g"; |
128 endif | 137 endif |
129 endif | 138 endif |
130 fmt = strcat (deblank (repmat (fmt, 1, columns (x))), "\n"); | 139 fmt = strcat (deblank (repmat (fmt, 1, columns (x))), "\n"); |
131 nd = ndims (x); | 140 nd = ndims (x); |
132 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); | 141 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); |
133 tmp(length (tmp)) = ""; | 142 tmp(length (tmp)) = ""; |
134 retval = split (tmp, "\n"); | 143 retval = strtrim (split (tmp, "\n")); |
135 endif | 144 endif |
136 | 145 |
137 endfunction | 146 endfunction |