Main Content

cast

Convert numeric value to different numeric data type

Description

y = cast(x,dataTypeName) converts numeric value x into the data type specified by dataTypeName.

In a chart that uses MATLAB as the action language, specify dataTypeName as "single", "double", "int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64", or "logical".

In a chart that uses C as the action language, specify dataTypeName as single, double, int8, uint8, int16, uint16, int32, uint32, int64, uint64, or boolean.

example

y = cast(x,"like",y) converts numeric value x into the same data type as numeric y in a chart that uses MATLAB as the action language.

example

y = cast(x,dataType) converts numeric x into dataType in a chart that uses C as the action language. Specify dataType as an expression that calls fixdt (Simulink) or type.

example

Examples

expand all

Cast the double x to an integer and assign the value to y.

In a chart that uses MATLAB as the action language, enter:

x = -4.56789;
y = cast(x,"int8");

Stateflow chart that uses the cast operator.

The value of y is -5.

In a chart that uses C as the action language, enter:

x = -4.56789;
y = cast(x,int8);

Stateflow chart that uses the cast operator.

The value of y is -4.

Cast the double x to the data type of integer z and assign the value to y.

In a chart that uses MATLAB as the action language, enter:

x = -4.56879;
z = int8(5);
y = cast(x,"like",z);

Stateflow chart that uses the cast operator.

The value of y is -5.

In a chart that uses C as the action language, enter:

x = -4.56789;
z = int8(5);
y = cast(x,type(z));

Stateflow chart that uses the type operator.

The value of y is -4.

In a chart that uses C as the action language, cast the double x to a fixed-point data type and assign the value to fp.

x = -4.56879;
fp = cast(x,fixdt(1,5,1));

Stateflow chart that uses the type operator.

The value of y is -4.5.

Version History

Introduced before R2006a

See Also

| (Simulink) | | |