annotate scripts/miscellaneous/cast.m @ 8920:eb63fbe60fab

update copyright notices
author John W. Eaton <jwe@octave.org>
date Sat, 07 Mar 2009 10:41:27 -0500
parents 9dca8b03dfe8
children 95c3e38098bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 7617
diff changeset
1 ## Copyright (C) 2007, 2008 John W. Eaton
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
2 ##
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
4 ##
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5011ac2fc23d [project @ 2007-03-13 14:45:51 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: 6403
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: 6403
diff changeset
8 ## your option) any later version.
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
9 ##
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
14 ##
5011ac2fc23d [project @ 2007-03-13 14:45:51 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: 6403
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: 6403
diff changeset
17 ## <http://www.gnu.org/licenses/>.
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
18
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
20 ## @deftypefn {Function File} {} cast (@var{val}, @var{type})
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
21 ## Convert @var{val} to data type @var{type}.
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
22 ## @seealso{int8, uint8, int16, uint16, int32, uint32, int64, uint64, double}
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
23 ## @end deftypefn
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
24
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
25 ## Author: jwe
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
26
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
27 function retval = cast (val, typ)
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
28
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
29 if (nargin == 2)
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
30 if (ischar (typ))
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
31 if (any (strcmp (typ, {"int8"; "uint8"; "int16"; "uint16";
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
32 "int32"; "uint32"; "int64"; "uint64";
7617
9dca8b03dfe8 Allow cast to 'char'.
David Bateman <dbateman@free.fr>
parents: 7473
diff changeset
33 "double"; "single"; "logical"; "char"})))
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
34 retval = feval (typ, val);
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
35 else
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
36 error ("cast: type name `%s' is not a built-in type", typ);
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
37 endif
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
38 else
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
39 error ("cast: expecting type name as second argument");
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
40 endif
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
41 else
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
42 print_usage ();
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
43 endif
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
44
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
45 endfunction