Main Content

Data Type Conversion

This example shows how to create a data type conversion by using a Data Type Conversion block, Stateflow Chart, or MATLAB Function block.

C Construct

y1 = (double)u1;

Modeling Pattern for Data Type Conversion — Simulink Block

To create a data type conversion, use a Data Type Conversion block from the Simulink > Commonly Used Blocks library.

1. Open example model ex_data_type_SL.

2. Click the Inport block. In the Property Inspector, under Signal Attributes, specify Data type as int32.

3. Click the Data Type Conversion block. In the Property Inspector, specify the Output data type parameter as double.

4. In the Configuration Parameters dialog box, set the Casting modes parameter to Explicit. By default, Simulink sets the Casting modes parameter to Nominal that generates minimal casting. For more details, see Control Cast Expressions in Generated Code.

5. To build the model and generate code, press Ctrl+B .

The generated code appears in ex_data_type_SL.c:

int32_T u1;                            /* '<Root>/u1' */
real_T y1;                             /* '<Root>/y1' */

/* Model step function */
void ex_data_type_SL_step(void)
{
  /* Outport: '<Root>/y1' incorporates:
   *  DataTypeConversion: '<Root>/Data Type Conversion'
   *  Inport: '<Root>/u1'
   */
  y1 = (real_T)u1;
}

The code generator type definition for double is real_T.

Modeling Pattern for Data Type Conversion — Stateflow Chart

You can use a Stateflow® chart in place of a Data Type Conversion block, to create a data type conversion.

1. Open example model ex_data_type_SF.

2. Click the Inport block. In the Property Inspector, under Signal Attributes, specify Data type as int32.

3. In the Configuration Parameters dialog box, set the Casting modes parameter to Explicit.

4. To build the model and generate code, press Ctrl+B .

The generated code appears in ex_data_type_SF.c:

int32_T u1;                            /* '<Root>/u1' */
real_T y1;                             /* '<Root>/Type_Conversion' */

/* Model step function */
void ex_data_type_SF_step(void)
{
  /* Chart: '<Root>/Type_Conversion' incorporates:
   *  Inport: '<Root>/u1'
   */
  y1 = (real_T)u1;
}

Modeling Pattern for Data Type Conversion — MATLAB Function Block

1. Open example model ex_data_type_ML.

2. The MATLAB Function Block contains this function:

function y1 = typeconv(u1)
y1 = double(u1); 
end

3. Click the Inport block. In the Property Inspector, under Signal Attributes, specify Data type as int32.

4. In the Configuration Parameters dialog box, set the Casting modes parameter to Explicit.

5. To build the model and generate code, press Ctrl+B .

The generated code appears in ex_data_type_ML.c:

int32_T u1;                            /* '<Root>/u1' */
real_T y1;                             /* '<Root>/MATLAB Function' */

/* Model step function */
void ex_data_type_ML_step(void)
{
  /* MATLAB Function: '<Root>/MATLAB Function' incorporates:
   *  Inport: '<Root>/u1'
   */
  y1 = (real_T)u1;
}

Other Type Conversions in Modeling

Type conversions can also occur on the output of blocks where the output variable is specified as a different data type. For example, in the Gain block, you can set the Parameter data type as Inherit via internal rule to control the output signal data type. Another example of type conversion can occur at the boundary of a Stateflow chart. You can specify the output variable as a different data type.

See Also

Related Topics