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 block dialog box, inspect the Parameter Attributes tab. Parameter data type 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.

At the command prompt, convert the numeric variable K1 to a Simulink.Parameter object.

K1 = Simulink.Parameter(2);

Configure K1 to appear in the generated code as a global variable by applying the storage class ExportedGlobal.

K1.CoderInfo.StorageClass = 'ExportedGlobal';

In the block dialog box, on the Main tab, click the action button next to the value of the Gain parameter. Select Open. The value of the Data type property is auto, which means the parameter object acquires its data type from the block parameters that use the object.

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

In the Gain block dialog box, on the Parameter Attributes tab, you can optionally set Parameter data type 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 this data type object as the data type of the parameter object.

K1.DataType = 'myType';

Use the data type object to set the output data type of the Inport block named In2. Due to data type propagation, the input signal of the Gain block also uses this data type.

set_param('rtwdemo_basicsc/In2','OutDataTypeStr','myType')

In the Gain block dialog box, on the Parameter Attributes tab, set Parameter data type to Inherit: Inherit from 'Gain'.

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