]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/cast.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / cast.m
1 ## Copyright (C) 2007-2012 John W. Eaton
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} cast (@var{val}, @var{type})
21 ## Convert @var{val} to data type @var{type}.
22 ## @seealso{int8, uint8, int16, uint16, int32, uint32, int64, uint64, double}
23 ## @end deftypefn
24
25 ## Author: jwe
26
27 function retval = cast (val, typ)
28
29   if (nargin == 2)
30     if (ischar (typ))
31       if (any (strcmp (typ, {"int8"; "uint8"; "int16"; "uint16";
32                              "int32"; "uint32"; "int64"; "uint64";
33                              "double"; "single"; "logical"; "char"})))
34         retval = feval (typ, val);
35       else
36         error ("cast: type name `%s' is not a built-in type", typ);
37       endif
38     else
39       error ("cast: expecting TYPE name as second argument");
40     endif
41   else
42     print_usage ();
43   endif
44
45 endfunction