Mercurial > octave
annotate scripts/general/int2str.m @ 22323:bac0d6f07a3e
maint: Update copyright notices for 2016.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 17 Aug 2016 01:05:19 -0400 |
parents | b433f9990452 |
children | 3a2b891d0b33 e9a0469dedd9 |
rev | line source |
---|---|
22323
bac0d6f07a3e
maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents:
21076
diff
changeset
|
1 ## Copyright (C) 1993-2016 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 -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
20 ## @deftypefn {} {} 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 ## int2str (123) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
27 ## @result{} "123" |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
28 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
29 ## 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
|
30 ## @result{} s = |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
31 ## 1 2 3 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
32 ## 4 5 6 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
33 ## |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
34 ## whos s |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
35 ## @result{} |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
36 ## 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
|
37 ## ==== ==== ==== ===== ===== |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
38 ## 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
|
39 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
40 ## @end example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
41 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
42 ## 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
|
43 ## results, use @code{sprintf} (@pxref{Formatted Output}). |
21057
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
44 ## |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
45 ## Programming Notes: |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
46 ## |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
47 ## Non-integers are rounded to integers before display. Only the real part |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
48 ## of complex numbers is displayed. |
21076
b433f9990452
strip trailing whitespace from files
John W. Eaton <jwe@octave.org>
parents:
21057
diff
changeset
|
49 ## |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
50 ## @seealso{sprintf, num2str, mat2str} |
3361 | 51 ## @end deftypefn |
4 | 52 |
2314 | 53 ## Author: jwe |
54 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
55 function retval = int2str (n) |
4 | 56 |
12780
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
57 if (nargin != 1) |
6046 | 58 print_usage (); |
21057
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
59 elseif (! (isnumeric (n) || islogical (n) || ischar (n))) |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
60 error ("int2str: N must be a numeric, logical, or character array"); |
4 | 61 endif |
62 | |
21057
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
63 if (ischar (n)) |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
64 retval = n; |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
65 return; |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
66 elseif (isempty (n)) |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
67 retval = ""; |
12780
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
68 return; |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
69 endif |
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
70 |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14402
diff
changeset
|
71 n = round (real (n)); |
21057
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
72 |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
73 ## Set up a suitable format string while ignoring Inf/NaN entries |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
74 nan_inf = ! isfinite (n(:)); |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
75 ndgt = floor (log10 (max (abs (n(! nan_inf))))); |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
76 if (isempty (ndgt) || ndgt == -Inf) |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
77 ndgt = 0; # All Inf or all zero array |
12780
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
78 endif |
4305 | 79 |
21057
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
80 ndgt += 3; |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
81 if (any (nan_inf)) |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
82 ndgt = max (ndgt, 5); |
4305 | 83 endif |
84 | |
21057
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
85 ## FIXME: Integers should be masked to show only 16 significant digits |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
86 fmt = sprintf ("%%%d.0f", ndgt); |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
87 |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
88 nd = ndims (n); |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
89 nc = columns (n) * (nd - 1); # ND-arrays are expanded in columns |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
90 n = permute (n, [2, 3:nd, 1]); |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
91 fmt = [repmat(fmt, 1, nc), "\n"]; |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
92 strtmp = sprintf (fmt, n); |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
93 retval = strtrim (char (ostrsplit (strtmp, "\n", true))); |
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
94 |
5642 | 95 endfunction |
7411 | 96 |
12780
c99714aeb008
int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
97 |
21057
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
98 %!assert (int2str (123), "123") |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
99 %!assert (int2str (-123), "-123") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
100 %!assert (int2str (1.2), "1") |
21057
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
101 %!assert (int2str (1.6), "2") |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
102 %!assert (int2str ([1, 2, 3; 4, 5, 6]), ["1 2 3";"4 5 6"]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
103 %!assert (int2str ([]), "") |
7411 | 104 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
105 %!error int2str () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
106 %!error int2str (1, 2) |
21057
b0afe1993268
Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
107 %!error <N must be a numeric> int2str ({1}) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
108 |