Main Content

Data Type Conversion

This example shows how to create a data type conversion by using a Data Type Conversion block. Alternatively, you can you can perform a data type conversion or inside a Stateflow® Chart or MATLAB® Function block, but doing so is less efficient.

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.

Data Type Conversion in a Stateflow Chart

You can perform a data type conversion inside a Stateflow chart. This example shows a model that uses a Stateflow chart to only perform a data type conversion, which is less efficient than using a Data Type Conversion block. However, if you need to perform a data type conversion in a model that uses a Stateflow Chart for other reasons, you can perform the conversion within the chart.

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;
}

Data Type Conversion in a MATLAB Function Block

You can perform a data type conversion inside a MATLAB Function block. This example shows a model that uses a MATLAB Function block to only perform a data type conversion, which is less efficient than using a Data Type Conversion block. However, if you need to perform a data type conversion in a model that uses a MATLAB Function block for other reasons, you can perform the conversion within the 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 u_1;                           /* '<Root>/u_1' */
real_T y_1;                            /* '<Root>/MATLAB Function' */

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

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