Main Content

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:

  1. Use variables instead of constants for block parameters.

  2. 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 share Simulink.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. See Generate Structured Text Code for Programmatically Created Tunable Parameters. This table shows the mapped tunable parameters and how they appear in the generated structured text code.

VariableStorage ClassGenerated Code (CODESYS 2.3)
K1Model default

K1 is a local function block variable.

FUNCTION_BLOCK SimpleSubsystem
.
.
VAR
    K1: LREAL := 0.1;
    .
    .
END_VAR
.
.
END_FUNCTION_BLOCK
K2ExportedGlobal

K2 is a global variable.

VAR_GLOBAL
    K2: LREAL := 0.2;
END_VAR
K3CoderInfo.CustomStorageClass set to Const.

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. See Generate Structured Text Code for Interactively Created Tunable Parameters. This table shows how the tunable parameters and how they appear in the generated structured text code.

VariableStorage ClassGenerated Code (CODESYS 2.3)
K1Model default

K1 is a local function block variable.

FUNCTION_BLOCK SimpleSubsystem
.
.
VAR
    K1: LREAL := 0.1;
    .
    .
END_VAR
.
.
END_FUNCTION_BLOCK
K2ExportedGlobal

K2 is a global variable.

VAR_GLOBAL
    K2: LREAL := 0.2;
END_VAR
K3CoderInfo.CustomStorageClass and Storage type qualifier set to Const.

K3 is a global constant.

VAR_GLOBAL CONSTANT
    SS_INITIALIZE: SINT := 0;
    K3: LREAL := 0.3;
    SS_STEP: SINT := 1;
END_VAR