Mercurial > hg > octave-lyh
annotate scripts/general/num2str.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | be55736a0783 |
children | c792872f8942 |
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 -*- |
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 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
28 ## example: |
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 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
45 ## ==== ==== ==== ===== ===== |
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 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
54 ## over the 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
|
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 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
57 ## will get unpredictable results. |
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 |
10549 | 78 if (isnumeric (x) && round (x) == x && abs (x) < (10 .^ arg)) |
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 | |
86 if (isnumeric (x) && round (x) == 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); | |
94 | |
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; | |
4878 | 114 idx = cell (); |
115 for i = 1:nd | |
7208 | 116 idx{i} = 1:sz(i); |
4878 | 117 endfor |
4914 | 118 idx{2} = perm; |
7208 | 119 x = horzcat (real (x), imag (x)); |
4914 | 120 x = x(idx{:}); |
5939 | 121 |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
122 fmt = cstrcat (deblank (repmat (fmt, 1, nc)), "\n"); |
4914 | 123 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); |
4878 | 124 |
125 ## Put the "i"'s where they are supposed to be. | |
126 while (true) | |
127 tmp2 = strrep (tmp, " i\n", "i\n"); | |
4914 | 128 if (length (tmp) == length (tmp2)) |
10549 | 129 break; |
4878 | 130 else |
10549 | 131 tmp = tmp2; |
4878 | 132 endif |
133 endwhile | |
134 while (true) | |
135 tmp2 = strrep (tmp, " i", "i "); | |
136 if (tmp == tmp2) | |
10549 | 137 break; |
4878 | 138 else |
10549 | 139 tmp = tmp2; |
4878 | 140 endif |
141 endwhile | |
142 | |
143 tmp(length (tmp)) = ""; | |
8877
2c8b2399247b
implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
8442
diff
changeset
|
144 retval = char (strtrim (strsplit (tmp, "\n"))); |
4878 | 145 else |
4229 | 146 if (nargin == 2) |
5443 | 147 if (ischar (arg)) |
10549 | 148 fmt = arg; |
4229 | 149 else |
10549 | 150 if (isnumeric (x) && round (x) == x && abs (x) < (10 .^ arg)) |
151 fmt = sprintf ("%%%dd ", arg); | |
152 else | |
153 fmt = sprintf ("%%%d.%dg", arg+7, arg); | |
154 endif | |
4229 | 155 endif |
4 | 156 else |
4691 | 157 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) |
10549 | 158 if (max (abs (x(:))) == 0) |
159 dgt = 2; | |
160 else | |
161 dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 2; | |
162 endif | |
163 if (length (abs (x) == x) > 0) | |
164 fmt = sprintf ("%%%dg ", dgt); | |
165 else | |
166 fmt = sprintf ("%%%dd ", dgt); | |
167 endif | |
4691 | 168 elseif (isscalar (x)) |
10549 | 169 fmt = "%11.5g"; |
4295 | 170 else |
10549 | 171 fmt = "%11.5g"; |
4295 | 172 endif |
4229 | 173 endif |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
174 fmt = cstrcat (deblank (repmat (fmt, 1, columns (x))), "\n"); |
4914 | 175 nd = ndims (x); |
176 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); | |
4878 | 177 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
|
178 retval = strtrim (char (strsplit (tmp, "\n"))); |
4 | 179 endif |
180 | |
181 endfunction | |
7411 | 182 |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
183 %!assert ((strcmp (num2str (123), "123") && strcmp (num2str (1.23), "1.23"))); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
184 %!assert (num2str (123.456, 4), "123.5"); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
185 %!assert (all (num2str ([1, 1.34; 3, 3.56], "%5.1f") == ["1.0 1.3"; "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
|
186 %!assert (num2str (1.234 + 27.3i), "1.234+27.3i"); |
7411 | 187 %!error num2str (); |
188 %!error num2str (1, 2, 3); | |
189 |