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'

Extended Capabilities

Introduced before R2006a