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 num2str} |
4
|
29 |
2314
|
30 ## Author: jwe |
|
31 |
2311
|
32 function retval = int2str (x) |
4
|
33 |
2303
|
34 ## XXX FIXME XXX -- this will fail for very large values. |
419
|
35 |
4
|
36 if (nargin == 1) |
4229
|
37 x = round (x); |
4303
|
38 t = abs (x(:)); |
|
39 t = t(t != 0); |
|
40 if (isempty (t)) |
|
41 ## All zeros. |
|
42 fmt = "%3d"; |
|
43 else |
|
44 ## Maybe have some zeros. |
|
45 nan_inf = isinf (t) | isnan (t); |
|
46 if (any (nan_inf)) |
|
47 min_fw = 5; |
|
48 else |
|
49 min_fw = 3; |
|
50 endif |
|
51 t = t(! nan_inf); |
|
52 if (isempty (t)) |
|
53 ## Only zeros, Inf, and NaN. |
|
54 fmt = "%5d"; |
|
55 else |
|
56 ## Could have anything. |
|
57 fw = max (floor (max (log10 (t) + 3)), min_fw); |
|
58 fmt = sprintf ("%%%dd", fw); |
|
59 endif |
|
60 endif |
4229
|
61 fmt = strcat (repmat (fmt, 1, columns (x)), "\n"); |
|
62 tmp = sprintf (fmt, round (x.')); |
|
63 tmp(length (tmp)) = ""; |
|
64 retval = split (tmp, "\n"); |
4
|
65 else |
904
|
66 usage ("int2str (x)"); |
4
|
67 endif |
|
68 |
|
69 endfunction |