[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: casting
From: |
John W. Eaton |
Subject: |
Re: casting |
Date: |
Mon, 12 Mar 2007 23:50:22 -0400 |
On 12-Mar-2007, David Grohmann wrote:
|
| Thank you. I will give that a shot.
|
| Søren Hauberg wrote:
| > How about this function:
| >
| > function B = cast(A, newclass)
| > B = feval(newclass, A);
| > endfunction
To avoid the feval and to also be able to detect invalid class names,
I used the following:
function retval = cast (val, typ)
if (nargin == 2)
if (ischar (typ))
switch (typ)
case "int8"
retval = int8 (val);
case "uint8"
retval = uint8 (val);
case "int16"
retval = int16 (val);
case "uint16"
retval = uint16 (val);
case "int32"
retval = int32 (val);
case "uint32"
retval = uint32 (val);
case "int64"
retval = int64 (val);
case "uint64"
retval = uint64 (val);
case "double"
retval = double (val);
case "single"
retval = single (val);
otherwise
error ("cast: invalid type name `%s'", typ);
endswitch
else
error ("cast: expecting type name as second argument");
endif
else
print_usage ();
endif
endfunction
Thanks,
jwe
- casting, David Grohmann, 2007/03/09