Reuse Parameter Data in Different Data Type Contexts
When you use a Simulink.Parameter
object or a numeric MATLAB® variable to set two or more block parameter values, if the block parameters have different data types, you must explicitly specify the data type of the object or variable. For example, you cannot leave the data type of the parameter object at the default value, auto
.
Create and Configure Example Model
Create the model ex_paramdt_contexts
.
ex_paramdt_contexts
On the Modeling tab, click Model Data Editor.
In the Model Data Editor, on the Inports/Outports tab, use the Data Type column to set the data type of the In1
Inport block to single
and the data type of the In2
block to int8
.
On the Signals tab, set the data types of the Gain block outputs to Inherit: Same as input
.
On the Parameters tab, for the Gain parameters of the Gain blocks, set Data Type to Inherit: Same as input
.
For the Gain parameters, set Value to myGainParam
.
Alternatively, to configure the blocks, use these commands at the command prompt:
set_param('ex_paramdt_contexts/In1','OutDataTypeStr','single') set_param('ex_paramdt_contexts/In2','OutDataTypeStr','int8') set_param('ex_paramdt_contexts/Gain - single','Gain','myGainParam',... 'OutDataTypeStr','Inherit: Same as input',... 'ParamDataTypeStr','Inherit: Same as input') set_param('ex_paramdt_contexts/Gain - int8','Gain','myGainParam',... 'OutDataTypeStr','Inherit: Same as input',... 'ParamDataTypeStr','Inherit: Same as input')
In the Model Data Editor, for either Gain block parameter, click the cell in the Value column. Next to myGainParam
, click the action button (with three vertical dots) and select Create.
In the Create New Data dialog box, set Value to Simulink.Parameter(3)
and click Create. A Simulink.Parameter
object with value 3
appears in the model workspace.
In the myGainParam property dialog box, set Data type to int8
.
On the Code Generation
tab, click Configure in Coder App.
In the Code Mappings editor, set the storage class of myGainParam
to ExportedGlobal
. With the storage class ExportedGlobal
, the object appears in the generated code as a global variable.
Alternatively, to create and configure the parameter object, use these commands at the command prompt:
mws = get_param('ex_paramdt_contexts', 'modelworkspace'); mws.assignin('myGainParam',Simulink.Parameter(3)); setVariablePart(mws,'myGainParam.DataType','int8'); cm = coder.mapping.utils.create('ex_paramdt_contexts'); setModelParameter(cm,'myGainParam','StorageClass','ExportedGlobal');
In this model, you use the parameter object myGainParam
to set two block parameter values. The block parameters inherit different data types from the block input signals (single
or int8
). To use myGainParam
in these different data type contexts, you explicitly specify the data type of the parameter object by setting the DataType
property to int8
.
Match Parameter Object Data Type with Signal Data Type
Optionally, use a Simulink.NumericType
or Simulink.AliasType
object to set the parameter object data type and one of the signal data types. This technique eliminates unnecessary typecasts and shifts in the generated code due to a mismatch between the parameter object data type and the signal data type.
At the command prompt, create a Simulink.NumericType
object to represent the data type int8
.
sharedType_int8 = fixdt('int8');
In the Model Data Editor, on the Inports/Outports tab, set the data type of the In2
Inport block to sharedType_int8
.
On the Parameters tab, update the block diagram. The data table now contains a row that represents the parameter object, myGainParam
.
Use the Data Type column to set the data type of the parameter object to sharedType_int8
.
Alternatively, to configure the block and the object, use these commands at the command prompt:
myGainParam.DataType = 'sharedType_int8'; set_param('ex_paramdt_contexts/In2','OutDataTypeStr','sharedType_int8')
The parameter object and the signal use the data type int8
. To change this data type, adjust the properties of the data type object sharedType_int8
.
Generate and Inspect Code
Generate code from the model.
slbuild('ex_paramdt_contexts')
### Starting build procedure for: ex_paramdt_contexts ### Successful completion of build procedure for: ex_paramdt_contexts Build Summary Top model targets: Model Build Reason Status Build Duration ====================================================================================================================== ex_paramdt_contexts Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 9.3624s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 10.089s
The generated file ex_paramdt_contexts.c
defines the global variable myGainParam
by using the data type int8_T
, which corresponds to the data type int8
in Simulink®.
file = fullfile('ex_paramdt_contexts_grt_rtw','ex_paramdt_contexts.c'); coder.example.extractLines(file,'/* Exported block parameters */','int8_T myGainParam = 3;',1,1)
/* Exported block parameters */ int8_T myGainParam = 3; /* Variable: myGainParam
The generated code algorithm in the model step
function uses myGainParam
to calculate the outputs of the two Gain blocks. In the case of the Gain block whose input signal uses the data type single
, the code algorithm casts myGainParam
to the data type real32_T
, which corresponds to the data type single
in Simulink.
coder.example.extractLines(file,'/* Model step function */',... '/* Model initialize function */',1,0)
/* Model step function */ void ex_paramdt_contexts_step(void) { /* Outport: '<Root>/Out1' incorporates: * Gain: '<Root>/Gain - single' * Inport: '<Root>/In1' */ ex_paramdt_contexts_Y.Out1 = (real32_T)myGainParam * ex_paramdt_contexts_U.In1; /* Outport: '<Root>/Out2' incorporates: * Gain: '<Root>/Gain - int8' * Inport: '<Root>/In2' */ ex_paramdt_contexts_Y.Out2 = (int8_T)(myGainParam * ex_paramdt_contexts_U.In2); }