Mercurial > hg > octave-nkf
annotate scripts/general/int2str.m @ 9576:d73c9bd101ef
fix int2str
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 27 Aug 2009 07:54:44 +0200 |
parents | eb63fbe60fab |
children | 95c3e38098bf |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, |
8920 | 2 ## 2004, 2005, 2006, 2007, 2008, 2009 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}) | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
22 ## 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
|
23 ## array). |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
24 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
25 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
26 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
27 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
28 ## int2str (123) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
29 ## @result{} "123" |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
30 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
31 ## s = int2str ([1, 2, 3; 4, 5, 6]) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
32 ## @result{} s = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
33 ## 1 2 3 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
34 ## 4 5 6 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
35 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
36 ## whos s |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
37 ## @result{} s = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
38 ## Attr Name Size Bytes Class |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
39 ## ==== ==== ==== ===== ===== |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
40 ## 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
|
41 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
42 ## @end example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
43 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
44 ## This function is not very flexible. For better control over the |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
45 ## results, use @code{sprintf} (@pxref{Formatted Output}). |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
46 ## @seealso{sprintf, num2str, mat2str} |
3361 | 47 ## @end deftypefn |
4 | 48 |
2314 | 49 ## Author: jwe |
50 | |
2311 | 51 function retval = int2str (x) |
4 | 52 |
53 if (nargin == 1) | |
4878 | 54 x = round (real(x)); |
55 sz = size(x); | |
56 nd = ndims (x); | |
4305 | 57 nc = columns (x); |
58 if (nc > 1) | |
4878 | 59 idx = cell (); |
60 for i = 1:nd | |
7208 | 61 idx{i} = 1:sz(i); |
4878 | 62 endfor |
63 idx(2) = 1; | |
64 ifmt = get_fmt (x(idx{:}), 0); | |
65 idx(2) = 2:sz(2); | |
66 rfmt = get_fmt (x(idx{:}), 2); | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
67 fmt = cstrcat (ifmt, repmat (rfmt, 1, nc-1), "\n"); |
4303 | 68 else |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
69 fmt = cstrcat (get_fmt (x, 0), "\n"); |
4303 | 70 endif |
4878 | 71 tmp = sprintf (fmt, permute (x, [2, 1, 3 : nd])); |
4305 | 72 tmp(end) = ""; |
8877
2c8b2399247b
implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
8442
diff
changeset
|
73 retval = char (strsplit (tmp, "\n")); |
4 | 74 else |
6046 | 75 print_usage (); |
4 | 76 endif |
77 | |
78 endfunction | |
4305 | 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. | |
9576 | 105 tfw = floor (log10 (double (abs (t)))) + 1 + sep; |
4309 | 106 fw = max (tfw); |
4305 | 107 if (any (t(tfw == fw) < 0)) |
108 fw++; | |
109 endif | |
110 fmt = sprintf ("%%%dd", max (fw, min_fw)); | |
111 endif | |
112 endif | |
113 | |
5642 | 114 endfunction |
7411 | 115 |
116 %!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
|
117 %!assert (all (int2str ([1, 2, 3; 4, 5, 6]) == ["1 2 3";"4 5 6"])); |
7411 | 118 %!error int2str (); |
119 %!error int2str (1, 2); | |
120 |