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 |
5053
|
28 ## |
3408
|
29 ## @seealso{sprintf and int2str} |
4
|
30 |
2314
|
31 ## Author: jwe |
|
32 |
4229
|
33 function retval = num2str (x, arg) |
4
|
34 |
4878
|
35 if (nargin != 1 && nargin != 2) |
|
36 usage ("num2str (x) or num2str (x, precision) or num2str (x, fmt)"); |
|
37 endif |
|
38 |
|
39 if (isstr (x)) |
|
40 retval = x; |
|
41 endif |
|
42 |
|
43 if (iscomplex (x)) |
|
44 if (nargin == 2) |
|
45 if (isstr (arg)) |
|
46 fmt = strcat (arg, "%-+", arg(2:end), "i"); |
|
47 else |
4919
|
48 if (isnumeric (x) && round (x) == x && abs (x) < (10 .^ arg)) |
4918
|
49 fmt = sprintf ("%%%dd%%-+%ddi ", arg, arg); |
|
50 else |
|
51 fmt = sprintf ("%%%d.%dg%%-+%d.%dgi", arg+7, arg, arg+7, arg); |
|
52 endif |
4878
|
53 endif |
|
54 else |
|
55 ## Setup a suitable format string |
|
56 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) |
|
57 dgt1 = ceil (log10 (max (max (abs (real (x(:)))), |
4914
|
58 max (abs (imag (x(:))))))) + 1; |
4878
|
59 dgt2 = dgt1 - (min (real (x(:))) >= 0); |
|
60 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); |
|
61 elseif (isscalar (x)) |
|
62 fmt = "%.4g%-+.4gi"; |
|
63 else |
|
64 fmt = "%11.4g%-+11.4gi"; |
|
65 endif |
|
66 endif |
|
67 |
|
68 ## Manipulate the complex value to have real values in the odd |
4914
|
69 ## columns and imaginary values in the even columns. |
4878
|
70 sz = size (x); |
4914
|
71 nc = sz(2); |
4878
|
72 nd = ndims (x); |
4914
|
73 perm = fix ([1:0.5:nc+0.5]); |
|
74 perm(2:2:2*nc) = perm(2:2:2*nc) + nc; |
4878
|
75 idx = cell (); |
|
76 for i = 1:nd |
|
77 idx {i} = 1:sz(i); |
|
78 endfor |
4914
|
79 idx{2} = perm; |
4878
|
80 x = horzcat (real(x), imag(x)); |
4914
|
81 x = x(idx{:}); |
4878
|
82 |
4914
|
83 fmt = strcat (deblank (repmat (fmt, 1, nc)), "\n"); |
|
84 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); |
4878
|
85 |
|
86 ## Put the "i"'s where they are supposed to be. |
|
87 while (true) |
|
88 tmp2 = strrep (tmp, " i\n", "i\n"); |
4914
|
89 if (length (tmp) == length (tmp2)) |
4878
|
90 break; |
|
91 else |
|
92 tmp = tmp2; |
|
93 endif |
|
94 endwhile |
|
95 while (true) |
|
96 tmp2 = strrep (tmp, " i", "i "); |
|
97 if (tmp == tmp2) |
|
98 break; |
|
99 else |
|
100 tmp = tmp2; |
|
101 endif |
|
102 endwhile |
|
103 |
|
104 tmp(length (tmp)) = ""; |
|
105 retval = split (tmp, "\n"); |
|
106 else |
4229
|
107 if (nargin == 2) |
|
108 if (isstr (arg)) |
|
109 fmt = arg; |
|
110 else |
4919
|
111 if (isnumeric (x) && round (x) == x && abs (x) < (10 .^ arg)) |
4918
|
112 fmt = sprintf ("%%%dd ", arg); |
|
113 else |
|
114 fmt = sprintf ("%%%d.%dg", arg+7, arg); |
|
115 endif |
4229
|
116 endif |
4
|
117 else |
4691
|
118 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) |
4914
|
119 if (max (abs (x(:))) == 0) |
4911
|
120 dgt = 1; |
|
121 else |
4914
|
122 dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 1; |
4911
|
123 endif |
4914
|
124 fmt = sprintf ("%%%dd ", dgt); |
4691
|
125 elseif (isscalar (x)) |
4295
|
126 fmt = "%.4g"; |
|
127 else |
|
128 fmt = "%11.4g"; |
|
129 endif |
4229
|
130 endif |
4878
|
131 fmt = strcat (deblank (repmat (fmt, 1, columns (x))), "\n"); |
4914
|
132 nd = ndims (x); |
|
133 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); |
4878
|
134 tmp(length (tmp)) = ""; |
|
135 retval = split (tmp, "\n"); |
4
|
136 endif |
|
137 |
|
138 endfunction |