]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/int2str.m
update packages
[CreaPhase.git] / octave_packages / m / general / int2str.m
1 ## Copyright (C) 1993-2012 John W. Eaton
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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} int2str (@var{n})
21 ## Convert an integer (or array of integers) to a string (or a character
22 ## array).
23 ##
24 ## @example
25 ## @group
26 ## int2str (123)
27 ##      @result{} "123"
28 ##
29 ## s = int2str ([1, 2, 3; 4, 5, 6])
30 ##      @result{} s =
31 ##         1  2  3
32 ##         4  5  6
33 ##
34 ## whos s
35 ##      @result{} s =
36 ##       Attr Name        Size                     Bytes  Class
37 ##       ==== ====        ====                     =====  =====
38 ##            s           2x7                         14  char
39 ## @end group
40 ## @end example
41 ##
42 ## This function is not very flexible.  For better control over the
43 ## results, use @code{sprintf} (@pxref{Formatted Output}).
44 ## @seealso{sprintf, num2str, mat2str}
45 ## @end deftypefn
46
47 ## Author: jwe
48
49 function retval = int2str (n)
50
51   if (nargin != 1)
52     print_usage ();
53   endif
54
55   if (isempty (n))
56     retval = '';
57     return;
58   endif
59
60   n = round (real(n));
61   sz = size(n);
62   nd = ndims (n);
63   nc = columns (n);
64   if (nc > 1)
65     idx = repmat ({':'}, nd, 1);
66     idx(2) = 1;
67     ifmt = get_fmt (n(idx{:}), 0);
68     idx(2) = 2:sz(2);
69     rfmt = get_fmt (n(idx{:}), 2);
70     fmt = cstrcat (ifmt, repmat (rfmt, 1, nc-1), "\n");
71   else
72     fmt = cstrcat (get_fmt (n, 0), "\n");
73   endif
74   tmp = sprintf (fmt, permute (n, [2, 1, 3 : nd]));
75   tmp(end) = "";
76   retval = char (strsplit (tmp, "\n"));
77
78 endfunction
79
80 function fmt = get_fmt (x, sep)
81
82   t = x(:);
83   t = t(t != 0);
84   if (isempty (t))
85     ## All zeros.
86     fmt = sprintf ("%%%dd", 1 + sep);
87   else
88     ## Maybe have some zeros.
89     nan_inf = isinf (t) | isnan (t);
90     if (any (nan_inf))
91       if (any (t(nan_inf) < 0))
92         min_fw = 4 + sep;
93       else
94         min_fw = 3 + sep;
95       endif
96     else
97       min_fw = 1 + sep;
98     endif
99     t = t(! nan_inf);
100     if (isempty (t))
101       ## Only zeros, Inf, and NaN.
102       fmt = sprintf ("%%%dd", min_fw);
103     else
104       ## Could have anything.
105       tfw = floor (log10 (double (abs (t)))) + 1 + sep;
106       fw = max (tfw);
107       if (any (t(tfw == fw) < 0))
108         fw++;
109       endif
110       fmt = sprintf ("%%%dd", max (fw, min_fw));
111     endif
112   endif
113
114 endfunction
115
116 %!assert (strcmp (int2str (-123), "-123") && strcmp (int2str (1.2), "1"));
117 %!assert (all (int2str ([1, 2, 3; 4, 5, 6]) == ["1  2  3";"4  5  6"]));
118 %!assert (int2str([]), "");
119
120 %!error int2str ();
121 %!error int2str (1, 2);
122