Mercurial > hg > octave-lyh
annotate scripts/general/int2str.m @ 12676:2783fa95cab7
Use common code idiom for creating cell array for indexing ND-arrays
* int2str.m, interpft.m, num2str.m, postpad.m, prepad.m, shift.m, fftshift.m,
ifftshift.m, unwrap.m
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 15 May 2011 10:50:30 -0700 |
parents | c792872f8942 |
children | c99714aeb008 |
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 |
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 | 57 if (nc > 1) |
12676
2783fa95cab7
Use common code idiom for creating cell array for indexing ND-arrays
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
58 idx = repmat ({':'}, nd, 1); |
4878 | 59 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
|
60 ifmt = get_fmt (n(idx{:}), 0); |
4878 | 61 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
|
62 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
|
63 fmt = cstrcat (ifmt, repmat (rfmt, 1, nc-1), "\n"); |
4303 | 64 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
|
65 fmt = cstrcat (get_fmt (n, 0), "\n"); |
4303 | 66 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
|
67 tmp = sprintf (fmt, permute (n, [2, 1, 3 : nd])); |
4305 | 68 tmp(end) = ""; |
8877
2c8b2399247b
implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
8442
diff
changeset
|
69 retval = char (strsplit (tmp, "\n")); |
4 | 70 else |
6046 | 71 print_usage (); |
4 | 72 endif |
73 | |
74 endfunction | |
4305 | 75 |
76 function fmt = get_fmt (x, sep) | |
77 | |
78 t = x(:); | |
79 t = t(t != 0); | |
80 if (isempty (t)) | |
81 ## All zeros. | |
82 fmt = sprintf ("%%%dd", 1 + sep); | |
83 else | |
84 ## Maybe have some zeros. | |
85 nan_inf = isinf (t) | isnan (t); | |
86 if (any (nan_inf)) | |
87 if (any (t(nan_inf) < 0)) | |
10549 | 88 min_fw = 4 + sep; |
4305 | 89 else |
10549 | 90 min_fw = 3 + sep; |
4305 | 91 endif |
92 else | |
93 min_fw = 1 + sep; | |
94 endif | |
95 t = t(! nan_inf); | |
96 if (isempty (t)) | |
97 ## Only zeros, Inf, and NaN. | |
98 fmt = sprintf ("%%%dd", min_fw); | |
99 else | |
100 ## Could have anything. | |
9576 | 101 tfw = floor (log10 (double (abs (t)))) + 1 + sep; |
4309 | 102 fw = max (tfw); |
4305 | 103 if (any (t(tfw == fw) < 0)) |
10549 | 104 fw++; |
4305 | 105 endif |
106 fmt = sprintf ("%%%dd", max (fw, min_fw)); | |
107 endif | |
108 endif | |
109 | |
5642 | 110 endfunction |
7411 | 111 |
112 %!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
|
113 %!assert (all (int2str ([1, 2, 3; 4, 5, 6]) == ["1 2 3";"4 5 6"])); |
7411 | 114 %!error int2str (); |
115 %!error int2str (1, 2); | |
116 |