Mercurial > hg > octave-nkf
annotate scripts/general/int2str.m @ 7926:d74f996e005d
__magick_read__.cc: configuration and style fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 14 Jul 2008 16:00:09 -0400 |
parents | 3422f39573b1 |
children | 502e58a0d44f |
rev | line source |
---|---|
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 -*- |
21 ## @deftypefn {Function File} {} int2str (@var{n}) | |
6623 | 22 ## Convert an integer to a string. This function is not very flexible. |
6555 | 23 ## For better control over the results, use @code{sprintf} |
24 ## (@pxref{Formatted Output}). | |
5642 | 25 ## @seealso{sprintf, num2str} |
3361 | 26 ## @end deftypefn |
4 | 27 |
2314 | 28 ## Author: jwe |
29 | |
2311 | 30 function retval = int2str (x) |
4 | 31 |
32 if (nargin == 1) | |
4878 | 33 x = round (real(x)); |
34 sz = size(x); | |
35 nd = ndims (x); | |
4305 | 36 nc = columns (x); |
37 if (nc > 1) | |
4878 | 38 idx = cell (); |
39 for i = 1:nd | |
7208 | 40 idx{i} = 1:sz(i); |
4878 | 41 endfor |
42 idx(2) = 1; | |
43 ifmt = get_fmt (x(idx{:}), 0); | |
44 idx(2) = 2:sz(2); | |
45 rfmt = get_fmt (x(idx{:}), 2); | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
46 fmt = cstrcat (ifmt, repmat (rfmt, 1, nc-1), "\n") |
4303 | 47 else |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
48 fmt = cstrcat (get_fmt (x, 0), "\n"); |
4303 | 49 endif |
4878 | 50 tmp = sprintf (fmt, permute (x, [2, 1, 3 : nd])); |
4305 | 51 tmp(end) = ""; |
4229 | 52 retval = split (tmp, "\n"); |
4 | 53 else |
6046 | 54 print_usage (); |
4 | 55 endif |
56 | |
57 endfunction | |
4305 | 58 |
59 function fmt = get_fmt (x, sep) | |
60 | |
61 t = x(:); | |
62 t = t(t != 0); | |
63 if (isempty (t)) | |
64 ## All zeros. | |
65 fmt = sprintf ("%%%dd", 1 + sep); | |
66 else | |
67 ## Maybe have some zeros. | |
68 nan_inf = isinf (t) | isnan (t); | |
69 if (any (nan_inf)) | |
70 if (any (t(nan_inf) < 0)) | |
71 min_fw = 4 + sep; | |
72 else | |
73 min_fw = 3 + sep; | |
74 endif | |
75 else | |
76 min_fw = 1 + sep; | |
77 endif | |
78 t = t(! nan_inf); | |
79 if (isempty (t)) | |
80 ## Only zeros, Inf, and NaN. | |
81 fmt = sprintf ("%%%dd", min_fw); | |
82 else | |
83 ## Could have anything. | |
84 tfw = floor (log10 (abs (t))) + 1 + sep; | |
4309 | 85 fw = max (tfw); |
4305 | 86 if (any (t(tfw == fw) < 0)) |
87 fw++; | |
88 endif | |
89 fmt = sprintf ("%%%dd", max (fw, min_fw)); | |
90 endif | |
91 endif | |
92 | |
5642 | 93 endfunction |
7411 | 94 |
95 %!assert(strcmp (int2str (-123), "-123") && strcmp (int2str (1.2), "1")); | |
96 | |
97 %!error int2str (); | |
98 | |
99 %!error int2str (1, 2); | |
100 |