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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, 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}). |
5642
|
27 ## @seealso{sprintf, num2str} |
3361
|
28 ## @end deftypefn |
4
|
29 |
2314
|
30 ## Author: jwe |
|
31 |
2311
|
32 function retval = int2str (x) |
4
|
33 |
|
34 if (nargin == 1) |
4878
|
35 x = round (real(x)); |
|
36 sz = size(x); |
|
37 nd = ndims (x); |
4305
|
38 nc = columns (x); |
|
39 if (nc > 1) |
4878
|
40 idx = cell (); |
|
41 for i = 1:nd |
|
42 idx {i} = 1:sz(i); |
|
43 endfor |
|
44 idx(2) = 1; |
|
45 ifmt = get_fmt (x(idx{:}), 0); |
|
46 idx(2) = 2:sz(2); |
|
47 rfmt = get_fmt (x(idx{:}), 2); |
4305
|
48 fmt = strcat (ifmt, repmat (rfmt, 1, nc-1), "\n") |
4303
|
49 else |
4305
|
50 fmt = strcat (get_fmt (x, 0), "\n"); |
4303
|
51 endif |
4878
|
52 tmp = sprintf (fmt, permute (x, [2, 1, 3 : nd])); |
4305
|
53 tmp(end) = ""; |
4229
|
54 retval = split (tmp, "\n"); |
4
|
55 else |
6046
|
56 print_usage (); |
4
|
57 endif |
|
58 |
|
59 endfunction |
4305
|
60 |
|
61 function fmt = get_fmt (x, sep) |
|
62 |
|
63 t = x(:); |
|
64 t = t(t != 0); |
|
65 if (isempty (t)) |
|
66 ## All zeros. |
|
67 fmt = sprintf ("%%%dd", 1 + sep); |
|
68 else |
|
69 ## Maybe have some zeros. |
|
70 nan_inf = isinf (t) | isnan (t); |
|
71 if (any (nan_inf)) |
|
72 if (any (t(nan_inf) < 0)) |
|
73 min_fw = 4 + sep; |
|
74 else |
|
75 min_fw = 3 + sep; |
|
76 endif |
|
77 else |
|
78 min_fw = 1 + sep; |
|
79 endif |
|
80 t = t(! nan_inf); |
|
81 if (isempty (t)) |
|
82 ## Only zeros, Inf, and NaN. |
|
83 fmt = sprintf ("%%%dd", min_fw); |
|
84 else |
|
85 ## Could have anything. |
|
86 tfw = floor (log10 (abs (t))) + 1 + sep; |
4309
|
87 fw = max (tfw); |
4305
|
88 if (any (t(tfw == fw) < 0)) |
|
89 fw++; |
|
90 endif |
|
91 fmt = sprintf ("%%%dd", max (fw, min_fw)); |
|
92 endif |
|
93 endif |
|
94 |
5642
|
95 endfunction |