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.

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.

  1. Write a MATLAB script that defines the tunable parameters.

    The following script setup_tunable_params.m creates the constants K1, K2, and K3 as Simulink.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';
  2. 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.

    1. In the model window, go to the Modeling tab and select Model Properties from the Model Settings drop-down.

    2. In the Model Properties dialog box, on the Callbacks tab, select PreLoadFcn. Enter setup_tunable_params for Model pre-load function.

    3. On the Callbacks tab, select CloseFcn. Enter clear K1 K2 K3; for Model close function.

    Every time that you open the model, the variables K1, K2, and K3 are loaded into the base workspace. You can view the variables and their storage classes in the Model Explorer.

  3. Generate code and inspect it.

    VariableStorage ClassGenerated Code (3S 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.

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.

  1. Specify that the variables K1, K2, and K3 must be initialized before the model loads and that the MATLAB workspace must be cleared before the model closes.

    1. In the Modeling tab and select Model Properties from the Model Settings drop-down.

    2. In the Model Properties dialog box, on the Callbacks tab, select PreLoadFcn. Enter K1=0.1; K2=0.2; K3=0.3; for Model pre-load function.

    3. On the Callbacks tab, select CloseFcn. Enter clear K1 K2 K3; for Model close function.

  2. On the Modeling tab and select Model Settings to open the Configuration Parameters dialog box.

  3. Navigate to Optimization pane. Specify that all parameters must be inlined in the generated code. Select Inlined for Default Parameter Behavior.

  4. 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.

  5. 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.

  6. Generate code and inspect it.

    VariableStorage ClassGenerated Code (3S CoDeSys 2.3)
    K1SimulinkGlobal

    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