Mercurial > hg > octave-lyh
annotate scripts/general/num2str.m @ 14428:099bd779466c
test: Fix failing %!test in fileread.m
* fileread.m: Fix failing %!test.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Fri, 02 Mar 2012 22:14:27 -0800 |
parents | f3d52523cde1 |
children | f8e041f11b18 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13279
diff
changeset
|
1 ## Copyright (C) 1993-2012 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 -*- |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} num2str (@var{x}) |
4229 | 21 ## @deftypefnx {Function File} {} num2str (@var{x}, @var{precision}) |
22 ## @deftypefnx {Function File} {} num2str (@var{x}, @var{format}) | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
23 ## Convert a number (or array) to a string (or a character array). The |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
24 ## optional second argument may either give the number of significant |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
25 ## digits (@var{precision}) to be used in the output or a format |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
26 ## template string (@var{format}) as in @code{sprintf} (@pxref{Formatted |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
27 ## Output}). @code{num2str} can also handle complex numbers. For |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
28 ## example: |
8442
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 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
31 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
32 ## num2str (123.456) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
33 ## @result{} "123.46" |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
34 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
35 ## num2str (123.456, 4) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
36 ## @result{} "123.5" |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
37 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
38 ## s = num2str ([1, 1.34; 3, 3.56], "%5.1f") |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
39 ## @result{} s = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
40 ## 1.0 1.3 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
41 ## 3.0 3.6 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
42 ## whos s |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
43 ## @result{} |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
44 ## 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
|
45 ## ==== ==== ==== ===== ===== |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
46 ## s 2x8 16 char |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
47 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
48 ## num2str (1.234 + 27.3i) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
49 ## @result{} "1.234+27.3i" |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
50 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
51 ## @end example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
52 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
53 ## The @code{num2str} function is not very flexible. For better control |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
54 ## over the 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
|
55 ## Note that for complex @var{x}, the format string may only contain one |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
56 ## output conversion specification and nothing else. Otherwise, you |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
57 ## will get unpredictable results. |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
58 ## @seealso{sprintf, int2str, mat2str} |
3361 | 59 ## @end deftypefn |
4 | 60 |
2314 | 61 ## Author: jwe |
62 | |
4229 | 63 function retval = num2str (x, arg) |
4 | 64 |
4878 | 65 if (nargin != 1 && nargin != 2) |
5947 | 66 print_usage (); |
4878 | 67 endif |
68 | |
5443 | 69 if (ischar (x)) |
4878 | 70 retval = x; |
5947 | 71 elseif (isempty (x)) |
72 retval = ""; | |
73 elseif (iscomplex (x)) | |
4878 | 74 if (nargin == 2) |
5443 | 75 if (ischar (arg)) |
10549 | 76 fmt = cstrcat (arg, "%-+", arg(2:end), "i"); |
4878 | 77 else |
13279
984359717d71
Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
78 if (isnumeric (x) && x == fix (x) && abs (x) < (10 .^ arg)) |
10549 | 79 fmt = sprintf ("%%%dd%%-+%ddi ", arg, arg); |
80 else | |
81 fmt = sprintf ("%%%d.%dg%%-+%d.%dgi", arg+7, arg, arg+7, arg); | |
82 endif | |
4878 | 83 endif |
84 else | |
85 ## Setup a suitable format string | |
13279
984359717d71
Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
86 if (isnumeric (x) && x == fix (x) && abs (x) < 1e10) |
10549 | 87 if (max (abs (real (x(:)))) == 0) |
88 dgt1 = 2; | |
89 else | |
90 dgt1 = ceil (log10 (max (max (abs (real (x(:)))), | |
91 max (abs (imag (x(:))))))) + 2; | |
92 endif | |
93 dgt2 = dgt1 - (min (real (x(:))) >= 0); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
94 |
10549 | 95 if (length (abs (x) == x) > 0) |
96 fmt = sprintf("%%%dg%%+-%dgi ", dgt2, dgt1); | |
97 else | |
98 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); | |
99 endif | |
4878 | 100 elseif (isscalar (x)) |
10549 | 101 fmt = "%.6g%-+.6gi"; |
4878 | 102 else |
10549 | 103 fmt = "%11.6g%-+11.6gi"; |
4878 | 104 endif |
105 endif | |
106 | |
107 ## Manipulate the complex value to have real values in the odd | |
4914 | 108 ## columns and imaginary values in the even columns. |
4878 | 109 sz = size (x); |
4914 | 110 nc = sz(2); |
4878 | 111 nd = ndims (x); |
4914 | 112 perm = fix ([1:0.5:nc+0.5]); |
113 perm(2:2:2*nc) = perm(2:2:2*nc) + nc; | |
12676
2783fa95cab7
Use common code idiom for creating cell array for indexing ND-arrays
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
114 idx = repmat ({':'}, nd, 1); |
4914 | 115 idx{2} = perm; |
7208 | 116 x = horzcat (real (x), imag (x)); |
4914 | 117 x = x(idx{:}); |
5939 | 118 |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
119 fmt = cstrcat (deblank (repmat (fmt, 1, nc)), "\n"); |
4914 | 120 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); |
4878 | 121 |
122 ## Put the "i"'s where they are supposed to be. | |
123 while (true) | |
124 tmp2 = strrep (tmp, " i\n", "i\n"); | |
4914 | 125 if (length (tmp) == length (tmp2)) |
10549 | 126 break; |
4878 | 127 else |
10549 | 128 tmp = tmp2; |
4878 | 129 endif |
130 endwhile | |
131 while (true) | |
132 tmp2 = strrep (tmp, " i", "i "); | |
133 if (tmp == tmp2) | |
10549 | 134 break; |
4878 | 135 else |
10549 | 136 tmp = tmp2; |
4878 | 137 endif |
138 endwhile | |
139 | |
140 tmp(length (tmp)) = ""; | |
8877
2c8b2399247b
implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
8442
diff
changeset
|
141 retval = char (strtrim (strsplit (tmp, "\n"))); |
4878 | 142 else |
4229 | 143 if (nargin == 2) |
5443 | 144 if (ischar (arg)) |
10549 | 145 fmt = arg; |
4229 | 146 else |
13279
984359717d71
Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
147 if (isnumeric (x) && x == fix (x) && abs (x) < (10 .^ arg)) |
10549 | 148 fmt = sprintf ("%%%dd ", arg); |
149 else | |
150 fmt = sprintf ("%%%d.%dg", arg+7, arg); | |
151 endif | |
4229 | 152 endif |
4 | 153 else |
13279
984359717d71
Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
154 if (isnumeric (x) && x == fix (x) && abs (x) < 1e10) |
10549 | 155 if (max (abs (x(:))) == 0) |
156 dgt = 2; | |
157 else | |
158 dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 2; | |
159 endif | |
160 if (length (abs (x) == x) > 0) | |
161 fmt = sprintf ("%%%dg ", dgt); | |
162 else | |
163 fmt = sprintf ("%%%dd ", dgt); | |
164 endif | |
4691 | 165 elseif (isscalar (x)) |
10549 | 166 fmt = "%11.5g"; |
4295 | 167 else |
10549 | 168 fmt = "%11.5g"; |
4295 | 169 endif |
4229 | 170 endif |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
171 fmt = cstrcat (deblank (repmat (fmt, 1, columns (x))), "\n"); |
4914 | 172 nd = ndims (x); |
173 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); | |
4878 | 174 tmp(length (tmp)) = ""; |
8893
937b58380b6a
num2str.m: Trivial bug fix. Recent switch from split() to strsplit() produced cells rather than character data.
Ben Abbott <bpabbott@mac.com>
parents:
8886
diff
changeset
|
175 retval = strtrim (char (strsplit (tmp, "\n"))); |
4 | 176 endif |
177 | |
178 endfunction | |
7411 | 179 |
180 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
181 %!assert (num2str (123), "123") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
182 %!assert (num2str (1.23), "1.23") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
183 %!assert (num2str (123.456, 4), "123.5") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
184 %!assert (num2str ([1, 1.34; 3, 3.56], "%5.1f"), ["1.0 1.3"; "3.0 3.6"]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
185 %!assert (num2str (1.234 + 27.3i), "1.234+27.3i") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
186 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
187 %!error num2str () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
188 %!error num2str (1, 2, 3) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
189 |