Mercurial > octave-nkf
annotate scripts/strings/str2num.m @ 12945:0c74237b3479 stable
str2num.m: Add second output indicating state of the conversion.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Wed, 10 Aug 2011 19:11:02 -0400 |
parents | c792872f8942 |
children | 8abb190e6cc8 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1996-2011 Kurt Hornik |
2558 | 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. | |
2558 | 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/>. | |
2558 | 18 |
3361 | 19 ## -*- texinfo -*- |
12945
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
20 ## @deftypefn {Function File} {@var{x} =} str2num (@var{s}) |
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{x}, @var{state}] =} str2num (@var{s}) |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
22 ## Convert the string (or character array) @var{s} to a number (or an |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
23 ## array). Examples: |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
24 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
25 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
26 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
27 ## str2num("3.141596") |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
28 ## @result{} 3.141596 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
29 ## |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
30 ## str2num(["1, 2, 3"; "4, 5, 6"]); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
31 ## @result{} ans = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
32 ## 1 2 3 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
33 ## 4 5 6 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
34 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
35 ## @end example |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
36 ## |
12945
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
37 ## The optional second output, @var{state}, is logically true when the |
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
38 ## coversion is successful. If the conversion fails the numeric output, |
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
39 ## @var{n}, is empty and @var{state} is false. |
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
40 ## |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
41 ## @strong{Caution:} As @code{str2num} uses the @code{eval} function |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
42 ## to do the conversion, @code{str2num} will execute any code contained |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
43 ## in the string @var{s}. Use @code{str2double} instead if you want to |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
44 ## avoid the use of @code{eval}. |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
45 ## @seealso{str2double, eval} |
3361 | 46 ## @end deftypefn |
2558 | 47 |
48 ## Author: jwe | |
49 | |
12945
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
50 function [m, state] = str2num (s) |
2558 | 51 |
5443 | 52 if (nargin == 1 && ischar (s)) |
2558 | 53 [nr, nc] = size (s); |
54 sep = ";"; | |
55 sep = sep (ones (nr, 1), 1); | |
56 s = sprintf ("m = [%s];", reshape ([s, sep]', 1, nr * (nc + 1))); | |
12945
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
57 state = true; |
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
58 eval (s, "m = []; state = false;"); |
5443 | 59 if (ischar (m)) |
3180 | 60 m = []; |
12945
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
61 state = false; |
3180 | 62 endif |
2558 | 63 else |
6046 | 64 print_usage (); |
2558 | 65 endif |
66 | |
67 endfunction | |
7411 | 68 |
69 %!assert(str2num ("-1.3e2") == -130 && str2num ("[1, 2; 3, 4]") == [1, 2; 3, 4]); | |
70 | |
71 %!error str2num (); | |
72 | |
73 %!error str2num ("string", 1); | |
74 | |
12945
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
75 %!test |
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
76 %! [x, state] = str2num ("pi"); |
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
77 %! assert (state) |
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
78 %! [x, state] = str2num (tmpnam); |
0c74237b3479
str2num.m: Add second output indicating state of the conversion.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
79 %! assert (! state) |