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 |
|
47 fmt = sprintf ("%%.%dg%%-+.%dgi", arg); |
|
48 endif |
|
49 else |
|
50 ## Setup a suitable format string |
|
51 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) |
|
52 dgt1 = ceil (log10 (max (max (abs (real (x(:)))), |
|
53 max (abs (imag (x(:))))))) + 1; |
|
54 dgt2 = dgt1 - (min (real (x(:))) >= 0); |
|
55 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); |
|
56 elseif (isscalar (x)) |
|
57 fmt = "%.4g%-+.4gi"; |
|
58 else |
|
59 fmt = "%11.4g%-+11.4gi"; |
|
60 endif |
|
61 endif |
|
62 |
|
63 ## Manipulate the complex value to have real values in the odd |
|
64 ## columns and imaginary values in the even columns |
|
65 sz = size (x); |
|
66 nc = sz (2); |
|
67 nd = ndims (x); |
|
68 perm = fix ([1 : 0.5 : nc + 0.5]); |
|
69 perm (2 : 2 : 2*nc) = perm(2 : 2 : 2*nc) + nc; |
|
70 idx = cell (); |
|
71 for i = 1:nd |
|
72 idx {i} = 1:sz(i); |
|
73 endfor |
|
74 idx {2} = perm; |
|
75 x = horzcat (real(x), imag(x)); |
|
76 x = x (idx{:}); |
|
77 |
|
78 fmt = strcat (deblank(repmat (fmt, 1, nc)), "\n"); |
|
79 tmp = sprintf (fmt, permute (x, [2, 1, 3 : nd])); |
|
80 |
|
81 ## Put the "i"'s where they are supposed to be. |
|
82 while (true) |
|
83 tmp2 = strrep (tmp, " i\n", "i\n"); |
|
84 if (length(tmp) == length(tmp2)) |
|
85 break; |
|
86 else |
|
87 tmp = tmp2; |
|
88 endif |
|
89 endwhile |
|
90 while (true) |
|
91 tmp2 = strrep (tmp, " i", "i "); |
|
92 if (tmp == tmp2) |
|
93 break; |
|
94 else |
|
95 tmp = tmp2; |
|
96 endif |
|
97 endwhile |
|
98 |
|
99 tmp(length (tmp)) = ""; |
|
100 retval = split (tmp, "\n"); |
|
101 else |
4229
|
102 if (nargin == 2) |
|
103 if (isstr (arg)) |
|
104 fmt = arg; |
|
105 else |
|
106 fmt = sprintf ("%%.%dg", arg); |
|
107 endif |
4
|
108 else |
4691
|
109 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) |
4878
|
110 dgt = ceil(log10(max(abs(x(:)))))+ (min (real (x(:))) < 0); |
|
111 fmt = sprintf("%%%dd ",dgt); |
4691
|
112 elseif (isscalar (x)) |
4295
|
113 fmt = "%.4g"; |
|
114 else |
|
115 fmt = "%11.4g"; |
|
116 endif |
4229
|
117 endif |
4878
|
118 fmt = strcat (deblank (repmat (fmt, 1, columns (x))), "\n"); |
|
119 tmp = sprintf (fmt, permute (x, [2, 1, 3 : ndims(x)])); |
|
120 tmp(length (tmp)) = ""; |
|
121 retval = split (tmp, "\n"); |
4
|
122 endif |
|
123 |
|
124 endfunction |