comparison scripts/general/num2str.m @ 10549:95c3e38098bf

Untabify .m scripts
author Rik <code@nomad.inbox5.com>
date Fri, 23 Apr 2010 11:28:50 -0700
parents eb63fbe60fab
children be55736a0783
comparison
equal deleted inserted replaced
10548:479536c5bb10 10549:95c3e38098bf
72 elseif (isempty (x)) 72 elseif (isempty (x))
73 retval = ""; 73 retval = "";
74 elseif (iscomplex (x)) 74 elseif (iscomplex (x))
75 if (nargin == 2) 75 if (nargin == 2)
76 if (ischar (arg)) 76 if (ischar (arg))
77 fmt = cstrcat (arg, "%-+", arg(2:end), "i"); 77 fmt = cstrcat (arg, "%-+", arg(2:end), "i");
78 else 78 else
79 if (isnumeric (x) && round (x) == x && abs (x) < (10 .^ arg)) 79 if (isnumeric (x) && round (x) == x && abs (x) < (10 .^ arg))
80 fmt = sprintf ("%%%dd%%-+%ddi ", arg, arg); 80 fmt = sprintf ("%%%dd%%-+%ddi ", arg, arg);
81 else 81 else
82 fmt = sprintf ("%%%d.%dg%%-+%d.%dgi", arg+7, arg, arg+7, arg); 82 fmt = sprintf ("%%%d.%dg%%-+%d.%dgi", arg+7, arg, arg+7, arg);
83 endif 83 endif
84 endif 84 endif
85 else 85 else
86 ## Setup a suitable format string 86 ## Setup a suitable format string
87 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) 87 if (isnumeric (x) && round (x) == x && abs (x) < 1e10)
88 if (max (abs (real (x(:)))) == 0) 88 if (max (abs (real (x(:)))) == 0)
89 dgt1 = 2; 89 dgt1 = 2;
90 else 90 else
91 dgt1 = ceil (log10 (max (max (abs (real (x(:)))), 91 dgt1 = ceil (log10 (max (max (abs (real (x(:)))),
92 max (abs (imag (x(:))))))) + 2; 92 max (abs (imag (x(:))))))) + 2;
93 endif 93 endif
94 dgt2 = dgt1 - (min (real (x(:))) >= 0); 94 dgt2 = dgt1 - (min (real (x(:))) >= 0);
95 95
96 if (length (abs (x) == x) > 0) 96 if (length (abs (x) == x) > 0)
97 fmt = sprintf("%%%dg%%+-%dgi ", dgt2, dgt1); 97 fmt = sprintf("%%%dg%%+-%dgi ", dgt2, dgt1);
98 else 98 else
99 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); 99 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1);
100 endif 100 endif
101 elseif (isscalar (x)) 101 elseif (isscalar (x))
102 fmt = "%.6g%-+.6gi"; 102 fmt = "%.6g%-+.6gi";
103 else 103 else
104 fmt = "%11.6g%-+11.6gi"; 104 fmt = "%11.6g%-+11.6gi";
105 endif 105 endif
106 endif 106 endif
107 107
108 ## Manipulate the complex value to have real values in the odd 108 ## Manipulate the complex value to have real values in the odd
109 ## columns and imaginary values in the even columns. 109 ## columns and imaginary values in the even columns.
125 125
126 ## Put the "i"'s where they are supposed to be. 126 ## Put the "i"'s where they are supposed to be.
127 while (true) 127 while (true)
128 tmp2 = strrep (tmp, " i\n", "i\n"); 128 tmp2 = strrep (tmp, " i\n", "i\n");
129 if (length (tmp) == length (tmp2)) 129 if (length (tmp) == length (tmp2))
130 break; 130 break;
131 else 131 else
132 tmp = tmp2; 132 tmp = tmp2;
133 endif 133 endif
134 endwhile 134 endwhile
135 while (true) 135 while (true)
136 tmp2 = strrep (tmp, " i", "i "); 136 tmp2 = strrep (tmp, " i", "i ");
137 if (tmp == tmp2) 137 if (tmp == tmp2)
138 break; 138 break;
139 else 139 else
140 tmp = tmp2; 140 tmp = tmp2;
141 endif 141 endif
142 endwhile 142 endwhile
143 143
144 tmp(length (tmp)) = ""; 144 tmp(length (tmp)) = "";
145 retval = char (strtrim (strsplit (tmp, "\n"))); 145 retval = char (strtrim (strsplit (tmp, "\n")));
146 else 146 else
147 if (nargin == 2) 147 if (nargin == 2)
148 if (ischar (arg)) 148 if (ischar (arg))
149 fmt = arg; 149 fmt = arg;
150 else 150 else
151 if (isnumeric (x) && round (x) == x && abs (x) < (10 .^ arg)) 151 if (isnumeric (x) && round (x) == x && abs (x) < (10 .^ arg))
152 fmt = sprintf ("%%%dd ", arg); 152 fmt = sprintf ("%%%dd ", arg);
153 else 153 else
154 fmt = sprintf ("%%%d.%dg", arg+7, arg); 154 fmt = sprintf ("%%%d.%dg", arg+7, arg);
155 endif 155 endif
156 endif 156 endif
157 else 157 else
158 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) 158 if (isnumeric (x) && round (x) == x && abs (x) < 1e10)
159 if (max (abs (x(:))) == 0) 159 if (max (abs (x(:))) == 0)
160 dgt = 2; 160 dgt = 2;
161 else 161 else
162 dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 2; 162 dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 2;
163 endif 163 endif
164 if (length (abs (x) == x) > 0) 164 if (length (abs (x) == x) > 0)
165 fmt = sprintf ("%%%dg ", dgt); 165 fmt = sprintf ("%%%dg ", dgt);
166 else 166 else
167 fmt = sprintf ("%%%dd ", dgt); 167 fmt = sprintf ("%%%dd ", dgt);
168 endif 168 endif
169 elseif (isscalar (x)) 169 elseif (isscalar (x))
170 fmt = "%11.5g"; 170 fmt = "%11.5g";
171 else 171 else
172 fmt = "%11.5g"; 172 fmt = "%11.5g";
173 endif 173 endif
174 endif 174 endif
175 fmt = cstrcat (deblank (repmat (fmt, 1, columns (x))), "\n"); 175 fmt = cstrcat (deblank (repmat (fmt, 1, columns (x))), "\n");
176 nd = ndims (x); 176 nd = ndims (x);
177 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); 177 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd]));