cast

Cast variable to different data type

Syntax

B = cast(A,newclass)
B = cast(A,'like',p)

Description

B = cast(A,newclass) converts A to class newclass, where newclass is the name of a built-in data type compatible with A. The cast function truncates any values in A that are too large to map into newclass.

B = cast(A,'like',p) converts A to the same data type and sparsity as the variable p. If A and p are both real, then B is also real. Otherwise, B is complex.

Examples

collapse all

Convert an int8 value to uint8.

Define a scalar 8-bit integer.

a = int8(5);

Convert a to an unsigned 8-bit integer.

b = cast(a,'uint8');
class(b)
ans =

uint8

Define a complex fi object.

p = fi( [1+2i 3i],1,24,12);

Define a scalar 8–bit integer.

a = int8(5);

Convert a to the same data type and complexity as p.

b = cast(a,'like',p)
b = 

   5.0000 + 0.0000i

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 24
        FractionLength: 12

See Also

|

Introduced before R2006a

Was this topic helpful?