MATLAB Examples

Eliminate Unnecessary Typecasts and Shifts by Matching Data Types

These examples show how to generate efficient code by configuring a block parameter to use the same data type as a signal that the block operates on.

Contents

Store Data Type Information in Model

Open the example model rtwdemo_basicsc and configure it to show the generated names of blocks.

load_system('rtwdemo_basicsc')
set_param('rtwdemo_basicsc','HideAutomaticNames','off')
open_system('rtwdemo_basicsc')

Identify the Gain block in the model, which uses the base workspace variable K1 to set the value of the Gain parameter. The input signal of this block uses the data type single.

In the model, select View > Model Data Editor.

In the Model Data Editor, select the Parameters tab.

In the model, select the Gain block. In the Model Data Editor, the Data Type column shows that the data type of the Gain parameter of the block is set to Inherit: Same as input. With this setting, the Gain parameter of this block uses the same data type as the input signal.

In the Model Data Editor, set the Change view drop-down list to Code.

Update the block diagram. Now, in the Model Data Editor, the data table shows rows that correspond to workspace variables that the model uses, including K1.

In the Model Data Editor, find the row that corresponds to K1. For that row, in the Storage Class column, select Convert to parameter object. Simulink converts K1 to a Simulink.Parameter object.

Use the Storage Class column to apply the storage class ExportedGlobal. With this setting, the object appears in the generated code as a global variable.

In the Model Data Editor, set Change view to Design.

In the data table, for the row that represents K1, in the Data Type column, select auto. With this setting, the parameter object acquires its data type from the block parameters that use the object (in this case, the Gain block parameter).

Alternatively, to create and configure the object, use these commands at the command prompt:

K1 = Simulink.Parameter(2);
K1.CoderInfo.StorageClass = 'ExportedGlobal';

Generate code from the model.

rtwbuild('rtwdemo_basicsc')
### Starting build procedure for model: rtwdemo_basicsc
### Successful completion of build procedure for model: rtwdemo_basicsc

The generated file rtwdemo_basicsc.c defines the global variable K1 by using the data type real32_T, which corresponds to the data type single in Simulink.

file = fullfile('rtwdemo_basicsc_grt_rtw','rtwdemo_basicsc.c');
rtwdemodbtype(file,'/* Exported block parameters */','real32_T K1 = 2.0F;',1,1)
/* Exported block parameters */
real32_T K1 = 2.0F;                    /* Variable: K1

The generated code algorithm in the model step function uses K1 directly without typecasting.

rtwdemodbtype(file,' rtwdemo_basicsc_DW.X = K1',...
    ' rtCP_Table1_bp01Data, rtCP_Table1_tableData,',1,1)
    rtwdemo_basicsc_DW.X = K1 * look1_iflf_binlx(rtwdemo_basicsc_U.input2,
      rtCP_Table1_bp01Data, rtCP_Table1_tableData, 10U);

On the Model Data Editor Parameters tab, you can can optionally set the data type of the Gain block parameter to Inherit: Inherit via internal rule (the default). In this case, the block parameter chooses the same data type as the input signal, single. However, when you use Inherit: Inherit via internal rule, under other circumstances (for example, when you use fixed-point data types), the block parameter might choose a different data type.

Store Data Type Information in Parameter Object

When you use a Simulink.Parameter object to export or import parameter data from the generated code to your external code, for example by applying the storage class ImportedExtern, you can specify data type information in the parameter object. To match the data type of the parameter object with a signal data type, create a Simulink.NumericType or Simulink.AliasType object. You can strictly control the data type that the parameter object uses in the generated code, eliminating the risk that Simulink chooses a different data type when you make changes to the model.

At the command prompt, create a Simulink.NumericType object that represents the data type single.

myType = Simulink.NumericType;
myType.DataTypeMode = 'Single';

Use the Model Data Editor Data Type column to set these data types to myType:

  • The parameter object. Use the Parameters tab.
  • The Inport block named In2. Use the Inports/Outports tab. Due to data type propagation, the input signal of the Gain block also uses myType.

Use the Model Data Editor Parameters tab to set the data type of the Gain block parameter to Inherit: Inherit from 'Gain'. Use this data type object as the data type of the parameter object.

Alternatively, to configure the object and the blocks, use these commands at the command prompt:

K1.DataType = 'myType';
set_param('rtwdemo_basicsc/In2','OutDataTypeStr','myType')
set_param('rtwdemo_basicsc/Gain','ParamDataTypeStr','Inherit: Inherit from ''Gain''')

Generate code from the model.

rtwbuild('rtwdemo_basicsc')
### Starting build procedure for model: rtwdemo_basicsc
### Successful completion of build procedure for model: rtwdemo_basicsc

The global variable K1 continues to use the data type real32_T.

file = fullfile('rtwdemo_basicsc_grt_rtw','rtwdemo_basicsc.c');
rtwdemodbtype(file,'/* Exported block parameters */','real32_T K1 = 2.0F;',1,1)
/* Exported block parameters */
real32_T K1 = 2.0F;                    /* Variable: K1