Main Content

Simulation and Code Generation for Tunable Parameters

This example shows how to map tunable parameters that are defined as Simulink.Parameter objects in the MATLAB® workspace to structured text code.

Open Model

In this model:

  • K1 has 'Model default' storage class

  • K2 has 'ExportedGlobal' storage class

  • K3 has 'ExportedGlobal' storage class and 'Const' custom storage class

% define tunable parameters in base workspace as Simulink.Parameter objects

%   Copyright 2011-2019 The MathWorks, Inc.

% 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'; 

Generate PLC Code

Open the PLC Coder app, select the SimpleSubsystem block, and on the PLC Code tab click Generate PLC Code.

To generate code using the MATLAB command line, enter:

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.

Tunable Parameter Mapping in Generated Code

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 Also