Mercurial > hg > octave-lyh
annotate scripts/general/int2str.m @ 12780:c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
int2str.m: Return "" for null input [].
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 13 Jul 2011 17:28:26 -0700 |
parents | 2783fa95cab7 |
children | 72c96de7a403 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1993-2011 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
245 | 18 |
3361 | 19 ## -*- texinfo -*- |
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 | 46 ## @end deftypefn |
4 | 47 |
2314 | 48 ## Author: jwe |
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 | 51 |
12780
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
52 if (nargin != 1) |
6046 | 53 print_usage (); |
4 | 54 endif |
55 | |
12780
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
56 if (isempty (n)) |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
57 retval = ''; |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
58 return; |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
59 endif |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
60 |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
61 n = round (real(n)); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
62 sz = size(n); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
63 nd = ndims (n); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
64 nc = columns (n); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
65 if (nc > 1) |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
66 idx = repmat ({':'}, nd, 1); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
67 idx(2) = 1; |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
68 ifmt = get_fmt (n(idx{:}), 0); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
69 idx(2) = 2:sz(2); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
70 rfmt = get_fmt (n(idx{:}), 2); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
71 fmt = cstrcat (ifmt, repmat (rfmt, 1, nc-1), "\n"); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
72 else |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
73 fmt = cstrcat (get_fmt (n, 0), "\n"); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
74 endif |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
75 tmp = sprintf (fmt, permute (n, [2, 1, 3 : nd])); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
76 tmp(end) = ""; |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
77 retval = char (strsplit (tmp, "\n")); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
78 |
4 | 79 endfunction |
4305 | 80 |
81 function fmt = get_fmt (x, sep) | |
82 | |
83 t = x(:); | |
84 t = t(t != 0); | |
85 if (isempty (t)) | |
86 ## All zeros. | |
87 fmt = sprintf ("%%%dd", 1 + sep); | |
88 else | |
89 ## Maybe have some zeros. | |
90 nan_inf = isinf (t) | isnan (t); | |
91 if (any (nan_inf)) | |
92 if (any (t(nan_inf) < 0)) | |
10549 | 93 min_fw = 4 + sep; |
4305 | 94 else |
10549 | 95 min_fw = 3 + sep; |
4305 | 96 endif |
97 else | |
98 min_fw = 1 + sep; | |
99 endif | |
100 t = t(! nan_inf); | |
101 if (isempty (t)) | |
102 ## Only zeros, Inf, and NaN. | |
103 fmt = sprintf ("%%%dd", min_fw); | |
104 else | |
105 ## Could have anything. | |
9576 | 106 tfw = floor (log10 (double (abs (t)))) + 1 + sep; |
4309 | 107 fw = max (tfw); |
4305 | 108 if (any (t(tfw == fw) < 0)) |
10549 | 109 fw++; |
4305 | 110 endif |
111 fmt = sprintf ("%%%dd", max (fw, min_fw)); | |
112 endif | |
113 endif | |
114 | |
5642 | 115 endfunction |
7411 | 116 |
12780
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
117 %!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
|
118 %!assert (all (int2str ([1, 2, 3; 4, 5, 6]) == ["1 2 3";"4 5 6"])); |
12780
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
119 %!assert (int2str([]), ""); |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
120 |
7411 | 121 %!error int2str (); |
122 %!error int2str (1, 2); | |
123 |