This model shows how tunable parameters map to Structured Text by specifying them as Simulink.Parameter objects in MATLAB base workspace.
This model uses three parameters K1, K2 and K3 defined in the MATLAB base workspace as Simulink.Parameter object. These parameters are used in the Gain blocks:
In this model:
K1 has 'SimulinkGlobal' storage class
K2 has 'ExportedGlobal' storage class
K3 has 'ExportedGlobal' storage class and 'Const' custom storage class
The parameters and their storage classes have been defined using the following MATLAB script (setup_tunable_params.m) which is run at model load time:
% define tunable parameters in base workspace as Simulink.Parameter objects
% tunable parameter mapped to local variable K1 = Simulink.Parameter; K1.Value = 0.1; K1.StorageClass = 'SimulinkGlobal';
% tunable parameter mapped to global variable K2 = Simulink.Parameter; K2.Value = 0.2; K2.StorageClass = 'ExportedGlobal'; K2.CoderInfo.CustomStorageClass = 'Default';
% tunable parameter mapped to global constant K3 = Simulink.Parameter; K3.Value = 0.3; K3.StorageClass = 'ExportedGlobal'; K3.CoderInfo.CustomStorageClass = 'Const';
Now you can generate PLC Structured Text code for this subsystem by right-clicking on the subsystem block and select PLC Code -> Generate Code for Subsystem
Alternatively, you can use the following command generatedFiles = plcgeneratecode('plcdemo_tunable_params_slparamobj/SimpleSubsystem');
After the code generation, the Diagnostic Viewer window is displayed with hyperlinks to the generated code files. You can open the generated files by clicking on the links.
In the generated Structured Text code for compatible IDE targets:
K1 is mapped to a Function Block local variable
K2 is mapped to a global variable
K3 is mapped to a global constant
For the RSLogix 5000 Add On Instruction (AOI) format:
K1 is mapped to an AOI local tag
K2 and K3 are mapped to AOI input tags
For the RSLogix 5000 Routine format:
K1 is mapped to routine instance tag
K2 and K3 are mapped to global program tags
See the Simulink PLC Coder documentation on tunable parameter code generation for more information.