Mercurial > hg > octave-nkf
annotate scripts/general/num2str.m @ 8442:502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Mon, 05 Jan 2009 08:11:03 +0100 |
parents | 3422f39573b1 |
children | 2c8b2399247b |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, |
2 ## 2004, 2005, 2006, 2007 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 -*- |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
21 ## @deftypefn {Function File} {} num2str (@var{x}) |
4229 | 22 ## @deftypefnx {Function File} {} num2str (@var{x}, @var{precision}) |
23 ## @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
|
24 ## 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
|
25 ## 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
|
26 ## 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
|
27 ## 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
|
28 ## 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
|
29 ## example: |
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 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
32 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
33 ## num2str (123.456) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
34 ## @result{} "123.46" |
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 ## 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
|
37 ## @result{} "123.5" |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
38 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
39 ## 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
|
40 ## @result{} s = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
41 ## 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
|
42 ## 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
|
43 ## whos s |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
44 ## @result{} |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
45 ## 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
|
46 ## ==== ==== ==== ===== ===== |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
47 ## 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
|
48 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
49 ## 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
|
50 ## @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
|
51 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
52 ## @end example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
53 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
54 ## 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
|
55 ## 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
|
56 ## 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
|
57 ## 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
|
58 ## 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
|
59 ## @seealso{sprintf, int2str, mat2str} |
3361 | 60 ## @end deftypefn |
4 | 61 |
2314 | 62 ## Author: jwe |
63 | |
4229 | 64 function retval = num2str (x, arg) |
4 | 65 |
4878 | 66 if (nargin != 1 && nargin != 2) |
5947 | 67 print_usage (); |
4878 | 68 endif |
69 | |
5443 | 70 if (ischar (x)) |
4878 | 71 retval = x; |
5947 | 72 elseif (isempty (x)) |
73 retval = ""; | |
74 elseif (iscomplex (x)) | |
4878 | 75 if (nargin == 2) |
5443 | 76 if (ischar (arg)) |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
77 fmt = cstrcat (arg, "%-+", arg(2:end), "i"); |
4878 | 78 else |
4919 | 79 if (isnumeric (x) && round (x) == x && abs (x) < (10 .^ arg)) |
4918 | 80 fmt = sprintf ("%%%dd%%-+%ddi ", arg, arg); |
81 else | |
82 fmt = sprintf ("%%%d.%dg%%-+%d.%dgi", arg+7, arg, arg+7, arg); | |
83 endif | |
4878 | 84 endif |
85 else | |
86 ## Setup a suitable format string | |
87 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) | |
6997 | 88 if (max (abs (real (x(:)))) == 0) |
89 dgt1 = 2; | |
90 else | |
91 dgt1 = ceil (log10 (max (max (abs (real (x(:)))), | |
92 max (abs (imag (x(:))))))) + 2; | |
93 endif | |
4878 | 94 dgt2 = dgt1 - (min (real (x(:))) >= 0); |
6975 | 95 |
96 if (length (abs (x) == x) > 0) | |
97 fmt = sprintf("%%%dg%%+-%dgi ", dgt2, dgt1); | |
98 else | |
99 fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); | |
100 endif | |
4878 | 101 elseif (isscalar (x)) |
6975 | 102 fmt = "%.6g%-+.6gi"; |
4878 | 103 else |
6975 | 104 fmt = "%11.6g%-+11.6gi"; |
4878 | 105 endif |
106 endif | |
107 | |
108 ## Manipulate the complex value to have real values in the odd | |
4914 | 109 ## columns and imaginary values in the even columns. |
4878 | 110 sz = size (x); |
4914 | 111 nc = sz(2); |
4878 | 112 nd = ndims (x); |
4914 | 113 perm = fix ([1:0.5:nc+0.5]); |
114 perm(2:2:2*nc) = perm(2:2:2*nc) + nc; | |
4878 | 115 idx = cell (); |
116 for i = 1:nd | |
7208 | 117 idx{i} = 1:sz(i); |
4878 | 118 endfor |
4914 | 119 idx{2} = perm; |
7208 | 120 x = horzcat (real (x), imag (x)); |
4914 | 121 x = x(idx{:}); |
5939 | 122 |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
123 fmt = cstrcat (deblank (repmat (fmt, 1, nc)), "\n"); |
4914 | 124 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); |
4878 | 125 |
126 ## Put the "i"'s where they are supposed to be. | |
127 while (true) | |
128 tmp2 = strrep (tmp, " i\n", "i\n"); | |
4914 | 129 if (length (tmp) == length (tmp2)) |
4878 | 130 break; |
131 else | |
132 tmp = tmp2; | |
133 endif | |
134 endwhile | |
135 while (true) | |
136 tmp2 = strrep (tmp, " i", "i "); | |
137 if (tmp == tmp2) | |
138 break; | |
139 else | |
140 tmp = tmp2; | |
141 endif | |
142 endwhile | |
143 | |
144 tmp(length (tmp)) = ""; | |
6975 | 145 retval = strtrim (split (tmp, "\n")); |
4878 | 146 else |
4229 | 147 if (nargin == 2) |
5443 | 148 if (ischar (arg)) |
4229 | 149 fmt = arg; |
150 else | |
4919 | 151 if (isnumeric (x) && round (x) == x && abs (x) < (10 .^ arg)) |
4918 | 152 fmt = sprintf ("%%%dd ", arg); |
153 else | |
154 fmt = sprintf ("%%%d.%dg", arg+7, arg); | |
155 endif | |
4229 | 156 endif |
4 | 157 else |
4691 | 158 if (isnumeric (x) && round (x) == x && abs (x) < 1e10) |
4914 | 159 if (max (abs (x(:))) == 0) |
6997 | 160 dgt = 2; |
4911 | 161 else |
6997 | 162 dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 2; |
4911 | 163 endif |
6975 | 164 if (length (abs (x) == x) > 0) |
165 fmt = sprintf ("%%%dg ", dgt); | |
166 else | |
167 fmt = sprintf ("%%%dd ", dgt); | |
168 endif | |
4691 | 169 elseif (isscalar (x)) |
6975 | 170 fmt = "%11.5g"; |
4295 | 171 else |
6975 | 172 fmt = "%11.5g"; |
4295 | 173 endif |
4229 | 174 endif |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
175 fmt = cstrcat (deblank (repmat (fmt, 1, columns (x))), "\n"); |
4914 | 176 nd = ndims (x); |
177 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); | |
4878 | 178 tmp(length (tmp)) = ""; |
6975 | 179 retval = strtrim (split (tmp, "\n")); |
4 | 180 endif |
181 | |
182 endfunction | |
7411 | 183 |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7540
diff
changeset
|
184 %!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
|
185 %!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
|
186 %!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
|
187 %!assert (num2str (1.234 + 27.3i), "1.234+27.3i"); |
7411 | 188 %!error num2str (); |
189 %!error num2str (1, 2, 3); | |
190 |