annotate scripts/strings/str2num.m @ 7530:bb0f2353cff5

new cell array ctype mappers
author John W. Eaton <jwe@octave.org>
date Mon, 25 Feb 2008 19:01:30 -0500
parents 83a8781b529d
children 502e58a0d44f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1996, 1998, 1999, 2005, 2006, 2007 Kurt Hornik
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
2 ##
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
4 ##
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
9 ##
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
14 ##
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
18
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
19 ## -*- texinfo -*-
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
20 ## @deftypefn {Function File} {} str2num (@var{s})
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
21 ## Convert the string @var{s} to a number.
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
22 ## @end deftypefn
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
23
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
24 ## Author: jwe
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
25
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
26 function m = str2num (s)
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
27
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
28 if (nargin == 1 && ischar (s))
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
29 [nr, nc] = size (s);
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
30 sep = ";";
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
31 sep = sep (ones (nr, 1), 1);
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
32 s = sprintf ("m = [%s];", reshape ([s, sep]', 1, nr * (nc + 1)));
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
33 eval (s, "m = [];");
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
34 if (ischar (m))
3180
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2558
diff changeset
35 m = [];
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2558
diff changeset
36 endif
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
37 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5443
diff changeset
38 print_usage ();
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
39 endif
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
40
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
41 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
42
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
43 %!assert(str2num ("-1.3e2") == -130 && str2num ("[1, 2; 3, 4]") == [1, 2; 3, 4]);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
44
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
45 %!error str2num ();
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
46
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
47 %!error str2num ("string", 1);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
48