annotate scripts/general/int2str.m @ 11587:c792872f8942

all script files: untabify and strip trailing whitespace
author John W. Eaton <jwe@octave.org>
date Thu, 20 Jan 2011 17:35:29 -0500
parents fd0a3ac60b0e
children 2783fa95cab7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11469
diff changeset
1 ## Copyright (C) 1993-2011 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7005
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7005
diff changeset
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7005
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7005
diff changeset
17 ## <http://www.gnu.org/licenses/>.
245
16a24e76d6e0 [project @ 1993-12-03 02:00:15 by jwe]
jwe
parents: 4
diff changeset
18
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 2847
diff changeset
19 ## -*- texinfo -*-
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 2847
diff changeset
20 ## @deftypefn {Function File} {} int2str (@var{n})
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
21 ## Convert an integer (or array of integers) to a string (or a character
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
22 ## array).
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
23 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
24 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
25 ## @group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
26 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
27 ## int2str (123)
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
28 ## @result{} "123"
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
29 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
30 ## s = int2str ([1, 2, 3; 4, 5, 6])
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
31 ## @result{} s =
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
32 ## 1 2 3
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
33 ## 4 5 6
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
34 ##
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
35 ## whos s
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
36 ## @result{} s =
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
37 ## Attr Name Size Bytes Class
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
38 ## ==== ==== ==== ===== =====
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
39 ## s 2x7 14 char
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
40 ## @end group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
41 ## @end example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
42 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
43 ## This function is not very flexible. For better control over the
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
44 ## results, use @code{sprintf} (@pxref{Formatted Output}).
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
45 ## @seealso{sprintf, num2str, mat2str}
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 2847
diff changeset
46 ## @end deftypefn
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
47
2314
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
48 ## Author: jwe
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
49
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
50 function retval = int2str (n)
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
51
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
52 if (nargin == 1)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
53 n = round (real(n));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
54 sz = size(n);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
55 nd = ndims (n);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
56 nc = columns (n);
4305
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
57 if (nc > 1)
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4309
diff changeset
58 idx = cell ();
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4309
diff changeset
59 for i = 1:nd
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9576
diff changeset
60 idx{i} = 1:sz(i);
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4309
diff changeset
61 endfor
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4309
diff changeset
62 idx(2) = 1;
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
63 ifmt = get_fmt (n(idx{:}), 0);
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4309
diff changeset
64 idx(2) = 2:sz(2);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
65 rfmt = get_fmt (n(idx{:}), 2);
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
66 fmt = cstrcat (ifmt, repmat (rfmt, 1, nc-1), "\n");
4303
e15a96673976 [project @ 2003-01-23 03:03:08 by jwe]
jwe
parents: 4229
diff changeset
67 else
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
68 fmt = cstrcat (get_fmt (n, 0), "\n");
4303
e15a96673976 [project @ 2003-01-23 03:03:08 by jwe]
jwe
parents: 4229
diff changeset
69 endif
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
70 tmp = sprintf (fmt, permute (n, [2, 1, 3 : nd]));
4305
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
71 tmp(end) = "";
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents: 8442
diff changeset
72 retval = char (strsplit (tmp, "\n"));
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
73 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
74 print_usage ();
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
75 endif
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
76
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
77 endfunction
4305
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
78
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
79 function fmt = get_fmt (x, sep)
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
80
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
81 t = x(:);
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
82 t = t(t != 0);
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
83 if (isempty (t))
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
84 ## All zeros.
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
85 fmt = sprintf ("%%%dd", 1 + sep);
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
86 else
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
87 ## Maybe have some zeros.
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
88 nan_inf = isinf (t) | isnan (t);
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
89 if (any (nan_inf))
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
90 if (any (t(nan_inf) < 0))
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9576
diff changeset
91 min_fw = 4 + sep;
4305
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
92 else
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9576
diff changeset
93 min_fw = 3 + sep;
4305
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
94 endif
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
95 else
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
96 min_fw = 1 + sep;
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
97 endif
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
98 t = t(! nan_inf);
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
99 if (isempty (t))
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
100 ## Only zeros, Inf, and NaN.
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
101 fmt = sprintf ("%%%dd", min_fw);
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
102 else
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
103 ## Could have anything.
9576
d73c9bd101ef fix int2str
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
104 tfw = floor (log10 (double (abs (t)))) + 1 + sep;
4309
a9560cebae6e [project @ 2003-01-28 23:24:58 by jwe]
jwe
parents: 4305
diff changeset
105 fw = max (tfw);
4305
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
106 if (any (t(tfw == fw) < 0))
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9576
diff changeset
107 fw++;
4305
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
108 endif
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
109 fmt = sprintf ("%%%dd", max (fw, min_fw));
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
110 endif
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
111 endif
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
112
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
113 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
114
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
115 %!assert(strcmp (int2str (-123), "-123") && strcmp (int2str (1.2), "1"));
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
116 %!assert (all (int2str ([1, 2, 3; 4, 5, 6]) == ["1 2 3";"4 5 6"]));
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
117 %!error int2str ();
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
118 %!error int2str (1, 2);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
119