annotate scripts/strings/str2num.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 83a8781b529d
children eb63fbe60fab
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})
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
21 ## Convert the string (or character array) @var{s} to a number (or an
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
22 ## array). Examples:
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
23 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
24 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
25 ## @group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
26 ## str2num("3.141596")
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
27 ## @result{} 3.141596
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
28 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
29 ## 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
30 ## @result{} ans =
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
31 ## 1 2 3
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
32 ## 4 5 6
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
33 ## @end group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
34 ## @end example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
35 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
36 ## @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
37 ## 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
38 ## in the string @var{s}. Use @code{str2double} instead if you want to
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
39 ## avoid the use of @code{eval}.
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7411
diff changeset
40 ## @seealso{str2double, eval}
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
41 ## @end deftypefn
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
42
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
43 ## Author: jwe
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
44
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
45 function m = str2num (s)
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
46
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
47 if (nargin == 1 && ischar (s))
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
48 [nr, nc] = size (s);
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
49 sep = ";";
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
50 sep = sep (ones (nr, 1), 1);
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
51 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
52 eval (s, "m = [];");
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
53 if (ischar (m))
3180
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2558
diff changeset
54 m = [];
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2558
diff changeset
55 endif
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
56 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5443
diff changeset
57 print_usage ();
2558
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
58 endif
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
59
c097977bca6f [project @ 1996-12-02 02:56:01 by jwe]
jwe
parents:
diff changeset
60 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
61
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
62 %!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
63
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
64 %!error str2num ();
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
65
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
66 %!error str2num ("string", 1);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
67