7017
|
1 ## Copyright (C) 1996, 1998, 1999, 2005, 2006, 2007 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 -*- |
|
20 ## @deftypefn {Function File} {} str2num (@var{s}) |
|
21 ## Convert the string @var{s} to a number. |
|
22 ## @end deftypefn |
2558
|
23 |
|
24 ## Author: jwe |
|
25 |
|
26 function m = str2num (s) |
|
27 |
5443
|
28 if (nargin == 1 && ischar (s)) |
2558
|
29 [nr, nc] = size (s); |
|
30 sep = ";"; |
|
31 sep = sep (ones (nr, 1), 1); |
|
32 s = sprintf ("m = [%s];", reshape ([s, sep]', 1, nr * (nc + 1))); |
|
33 eval (s, "m = [];"); |
5443
|
34 if (ischar (m)) |
3180
|
35 m = []; |
|
36 endif |
2558
|
37 else |
6046
|
38 print_usage (); |
2558
|
39 endif |
|
40 |
|
41 endfunction |
7411
|
42 |
|
43 %!assert(str2num ("-1.3e2") == -130 && str2num ("[1, 2; 3, 4]") == [1, 2; 3, 4]); |
|
44 |
|
45 %!error str2num (); |
|
46 |
|
47 %!error str2num ("string", 1); |
|
48 |