2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
245
|
19 |
3361
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} int2str (@var{n}) |
4229
|
22 ## @deftypefnx {Function File} {} num2str (@var{x}, @var{precision}) |
|
23 ## @deftypefnx {Function File} {} num2str (@var{x}, @var{format}) |
3361
|
24 ## Convert a number to a string. These functions are not very flexible, |
|
25 ## but are provided for compatibility with @sc{Matlab}. For better control |
|
26 ## over the results, use @code{sprintf} (@pxref{Formatted Output}). |
|
27 ## @end deftypefn |
3408
|
28 ## @seealso{sprintf and int2str} |
4
|
29 |
2314
|
30 ## Author: jwe |
|
31 |
4229
|
32 function retval = num2str (x, arg) |
4
|
33 |
4878
|
34 if (nargin != 1 && nargin != 2) |
|
35 usage ("num2str (x) or num2str (x, precision) or num2str (x, fmt)"); |
|
36 endif |
|
37 |
|
38 if (isstr (x)) |
|
39 retval = x; |
|
40 endif |
|
41 |
|
42 if (iscomplex (x)) |
|
43 if (nargin == 2) |
|
44 if (isstr (arg)) |
|
45 fmt = strcat (arg, "%-+", arg(2:end), "i"); |
|
46 else |
4918
|
47 if (isnumeric (x) && round (x) == x && abs (x) < (10.^arg)) |
|
48 fmt = sprintf ("%%%dd%%-+%ddi ", arg, arg); |
|
49 else |
|
50 fmt = sprintf ("%%%d.%dg%%-+%d.%dgi", arg+7, arg, arg+7, arg); |
|
51 endif |
4878
|
52 endif |
|
53 else |
|
54 ## Setup a suitable format string |
|
55 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) |
|
56 dgt1 = ceil (log10 (max (max (abs (real (x(:)))), |
4914
|
57 max (abs (imag (x(:))))))) + 1; |
4878
|
58 dgt2 = dgt1 - (min (real (x(:))) >= 0); |
|
59 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); |
|
60 elseif (isscalar (x)) |
|
61 fmt = "%.4g%-+.4gi"; |
|
62 else |
|
63 fmt = "%11.4g%-+11.4gi"; |
|
64 endif |
|
65 endif |
|
66 |
|
67 ## Manipulate the complex value to have real values in the odd |
4914
|
68 ## columns and imaginary values in the even columns. |
4878
|
69 sz = size (x); |
4914
|
70 nc = sz(2); |
4878
|
71 nd = ndims (x); |
4914
|
72 perm = fix ([1:0.5:nc+0.5]); |
|
73 perm(2:2:2*nc) = perm(2:2:2*nc) + nc; |
4878
|
74 idx = cell (); |
|
75 for i = 1:nd |
|
76 idx {i} = 1:sz(i); |
|
77 endfor |
4914
|
78 idx{2} = perm; |
4878
|
79 x = horzcat (real(x), imag(x)); |
4914
|
80 x = x(idx{:}); |
4878
|
81 |
4914
|
82 fmt = strcat (deblank (repmat (fmt, 1, nc)), "\n"); |
|
83 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); |
4878
|
84 |
|
85 ## Put the "i"'s where they are supposed to be. |
|
86 while (true) |
|
87 tmp2 = strrep (tmp, " i\n", "i\n"); |
4914
|
88 if (length (tmp) == length (tmp2)) |
4878
|
89 break; |
|
90 else |
|
91 tmp = tmp2; |
|
92 endif |
|
93 endwhile |
|
94 while (true) |
|
95 tmp2 = strrep (tmp, " i", "i "); |
|
96 if (tmp == tmp2) |
|
97 break; |
|
98 else |
|
99 tmp = tmp2; |
|
100 endif |
|
101 endwhile |
|
102 |
|
103 tmp(length (tmp)) = ""; |
|
104 retval = split (tmp, "\n"); |
|
105 else |
4229
|
106 if (nargin == 2) |
|
107 if (isstr (arg)) |
|
108 fmt = arg; |
|
109 else |
4918
|
110 if (isnumeric (x) && round (x) == x && abs (x) < (10.^ arg)) |
|
111 fmt = sprintf ("%%%dd ", arg); |
|
112 else |
|
113 fmt = sprintf ("%%%d.%dg", arg+7, arg); |
|
114 endif |
4229
|
115 endif |
4
|
116 else |
4691
|
117 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) |
4914
|
118 if (max (abs (x(:))) == 0) |
4911
|
119 dgt = 1; |
|
120 else |
4914
|
121 dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 1; |
4911
|
122 endif |
4914
|
123 fmt = sprintf ("%%%dd ", dgt); |
4691
|
124 elseif (isscalar (x)) |
4295
|
125 fmt = "%.4g"; |
|
126 else |
|
127 fmt = "%11.4g"; |
|
128 endif |
4229
|
129 endif |
4878
|
130 fmt = strcat (deblank (repmat (fmt, 1, columns (x))), "\n"); |
4914
|
131 nd = ndims (x); |
|
132 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); |
4878
|
133 tmp(length (tmp)) = ""; |
|
134 retval = split (tmp, "\n"); |
4
|
135 endif |
|
136 |
|
137 endfunction |