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