Control Appearance of Block Parameters in Generated Code
Unless you use constants for block parameters in your model, they appear in the generated code as variables. You can choose how these variables appear in the generated code. For more information, see Block Parameters in Generated Code.
To control how the block parameters appear in the generated code:
Use variables instead of constants for block parameters.
Define these parameters in the MATLAB® workspace in one of the following ways:
Use a MATLAB script to create a
Simulink.Parameter
object. Run the script every time that the model loads.Simulink® stores
Simulink.Parameter
objects outside the model. You can then shareSimulink.Parameter
objects between multiple models.Use the Model Configuration Parameters dialog box to make the parameters tunable.
Simulink stores global tunable parameters specified using the Configuration Parameters dialog box with the model. You cannot share these parameters between multiple models.
Note
The MATLAB workspace parameter value must be of the same data type as used in the model. Otherwise, the value of the variable in the generated code is set to zero. See Workspace Parameter Data Type Limitations.
Configure Tunable Parameters with Simulink.Parameter
Objects
This example shows how to create and modify a Simulink.Parameter
object.
The model plcdemo_tunable_params_slparamobj
illustrates these
steps. The model contains a Subsystem block SimpleSubsystem
that
has three Gain blocks with tunable parameters, K1
,
K2
, and K3
.
Write a MATLAB script that defines the tunable parameters.
The following script
setup_tunable_params.m
creates the constantsK1
,K2
, andK3
asSimulink.Parameter
objects, assigns values, and sets the storage classes for these constants. For more information on the storage classes, see Block Parameters in Generated Code.% tunable parameter mapped to local variable K1 = Simulink.Parameter; K1.Value = 0.1; K1.CoderInfo.StorageClass = 'Model default'; % tunable parameter mapped to global variable K2 = Simulink.Parameter; K2.Value = 0.2; K2.CoderInfo.StorageClass = 'ExportedGlobal'; % tunable parameter mapped to global const K3 = Simulink.Parameter; K3.Value = 0.3; K3.CoderInfo.StorageClass = 'Custom'; K3.CoderInfo.CustomStorageClass = 'Const';
Specify that the script
setup_tunable_params.m
must execute before the model loads and that the MATLAB workspace must be cleared before the model closes.In the model window, go to the Modeling tab and select Model Properties from the Model Settings drop-down.
In the Model Properties dialog box, on the Callbacks tab, select
PreLoadFcn
. Entersetup_tunable_params
for Model pre-load function.On the Callbacks tab, select
CloseFcn
. Enterclear K1 K2 K3;
for Model close function.
Every time that you open the model, the variables
K1
,K2
, andK3
are loaded into the base workspace. You can view the variables and their storage classes in the Model Explorer.Generate code and inspect it.
Variable Storage Class Generated Code (3S CoDeSys 2.3) K1
Model default
K1
is a local function block variable.FUNCTION_BLOCK SimpleSubsystem . . VAR K1: LREAL := 0.1; . . END_VAR . . END_FUNCTION_BLOCK
K2
ExportedGlobal
K2
is a global variable.VAR_GLOBAL K2: LREAL := 0.2; END_VAR
K3
CoderInfo.CustomStorageClass
set toConst
.K3
is a global constant.VAR_GLOBAL CONSTANT SS_INITIALIZE: SINT := 0; K3: LREAL := 0.3; SS_STEP: SINT := 1; END_VAR
Make Parameters Tunable Using Configuration Parameters Dialog Box
This example shows how to make parameters tunable using the Model Configuration Parameters dialog box.
The model plcdemo_tunable_params
illustrates these steps. The
model contains a Subsystem block SimpleSubsystem
that has three
Gain blocks with tunable parameters, K1
, K2
,
and K3
.
Specify that the variables
K1
,K2
, andK3
must be initialized before the model loads and that the MATLAB workspace must be cleared before the model closes.In the Modeling tab and select Model Properties from the Model Settings drop-down.
In the Model Properties dialog box, on the Callbacks tab, select
PreLoadFcn
. EnterK1=0.1; K2=0.2; K3=0.3;
for Model pre-load function.On the Callbacks tab, select
CloseFcn
. Enterclear K1 K2 K3;
for Model close function.
On the Modeling tab and select Model Settings to open the Configuration Parameters dialog box.
Navigate to Optimization pane. Specify that all parameters must be inlined in the generated code. Select
Inlined
for Default Parameter Behavior.To override the inlining and make individual parameters tunable, click Configure. In the Model Parameter Configuration dialog box, from the Source list, select
Referenced workspace variables
.Ctrl+select the parameters and click Add to table >>.
By default, this dialog box sets all parameters to the
SimulinkGlobal
storage class. Set the Storage class and Storage type qualifier as shown in this figure. For more information on the storage classes, see Block Parameters in Generated Code.Generate code and inspect it.
Variable Storage Class Generated Code (3S CoDeSys 2.3) K1
Model default
K1
is a local function block variable.FUNCTION_BLOCK SimpleSubsystem . . VAR K1: LREAL := 0.1; . . END_VAR . . END_FUNCTION_BLOCK
K2
ExportedGlobal
K2
is a global variable.VAR_GLOBAL K2: LREAL := 0.2; END_VAR
K3
CoderInfo.CustomStorageClass
and Storage type qualifier set toConst
.K3
is a global constant.VAR_GLOBAL CONSTANT SS_INITIALIZE: SINT := 0; K3: LREAL := 0.3; SS_STEP: SINT := 1; END_VAR