Main Content

Declare Workspace Variables as Tunable Parameters Using the Model Parameter Configuration Dialog Box

You can use the Model Parameter Configuration dialog box to declare numeric MATLAB® variables in the base workspace as tunable parameters. You can select code generation options, such as storage class, for each tunable parameter.

However, it is a best practice to instead use parameter objects to declare tunable parameters. Do not use the Model Parameter Configuration dialog box to select parameter objects in the base workspace. To use parameter objects, instead of the Model Parameter Configuration dialog box, to declare tunable parameters, see C Code Generation Configuration for Model Interface Elements.

Note

You cannot use the Model Parameter Configuration dialog box to declare tunable parameters for a referenced model. Use Simulink.Parameter objects instead.

Declare Existing Workspace Variables as Tunable Parameters

Use the Model Parameter Configuration dialog box to declare existing workspace variables as tunable parameters for a model.

  1. In the Configuration Parameters dialog box, on the Optimization pane, set the Default parameter behavior parameter to Tunable and click Configure.

  2. In the Model Parameter Configuration dialog box, under Source list, select a method to populate the list of available workspace variables.

    • Select MATLAB workspace to view numeric variables that are defined in the base workspace.

    • Select Referenced workspace variables to view only the numeric variables in the base workspace that the model uses. Selecting this option begins a diagram update and a search for used variables, which can take time for a large model.

  3. In the Model Parameter Configuration dialog box, under Source list, select one or more workspace variables.

  4. Click Add to table. The variables appear as tunable parameters under Global (tunable) parameters, and appear in italic font under Source list.

  5. Optionally, select a parameter under Global (tunable) parameters, and adjust the code generation settings for the parameter. For more information about adjusting the code generation options for tunable parameters, see Set Tunable Parameter Code Generation Options

  6. Click OK to apply your selection of tunable parameters and close the dialog box.

Declare New Tunable Parameters

Use the Model Parameter Configuration dialog box to declare new tunable parameters. You can use this technique to declare the names of tunable parameters, and to adjust their code generation settings, before you create the corresponding workspace variables.

  1. In the Configuration Parameters dialog box, on the Optimization pane, set the Default parameter behavior parameter to Tunable and click Configure.

  2. In the Model Parameter Configuration dialog box, under Global (tunable) parameters, click New.

  3. Under the Name column, specify a name for the new tunable parameter.

  4. Optionally, adjust the code generation settings for the new parameter. For more information about adjusting the code generation options for tunable parameters, see Set Tunable Parameter Code Generation Options

  5. Click OK to apply your changes and close the dialog box.

Set Tunable Parameter Code Generation Options

To set the properties of tunable parameters listed under Global (tunable) parameters in the Model Parameter Configuration dialog box, select a parameter and specify a storage class and, optionally, a storage type qualifier.

PropertyDescription
Storage class

Select one of the following to use for code generation:

  • Model default

  • ExportedGlobal

  • ImportedExtern

  • ImportedExternPointer

For more information about tunable parameter storage classes, see C Code Generation Configuration for Model Interface Elements.

Storage type qualifier

For variables with a storage class other than Auto, you can add a qualifier (such as const or volatile) to the generated storage declaration. To do so, you can select a predefined qualifier from the list, or add qualifiers not in the list by typing them in. The code generator does not check the storage type qualifier for validity, and includes the qualifier text in the generated code without checking syntax .

Programmatically Declare Workspace Variables as Tunable Parameters

Tune Parameters from the Command Line

When parameters are MATLAB workspace variables, the Model Parameter Configuration dialog box is the recommended way to see or set the properties of tunable parameters. In addition to that dialog box, you can also use MATLAB get_param and set_param commands.

Note

You can also use Simulink.Parameter objects for tunable parameters. See Create Tunable Calibration Parameter in the Generated Code for details.

The following commands return the tunable parameters and corresponding properties:

  • get_param(gcs,'TunableVars')

  • get_param(gcs,'TunableVarsStorageClass')

  • get_param(gcs,'TunableVarsTypeQualifier')

The following commands declare tunable parameters or set corresponding properties:

  • set_param(gcs,'TunableVars',str)

    The argument str (character vector) is a comma-separated list of variable names.

  • set_param(gcs,'TunableVarsStorageClass',str)

    The argument str (character vector) is a comma-separated list of storage class settings.

    The valid storage class settings are

    • Auto

    • ExportedGlobal

    • ImportedExtern

    • ImportedExternPointer

  • set_param(gcs,'TunableVarsTypeQualifier',str)

    The argument str (character vector) is a comma-separated list of storage type qualifiers.

The following example declares the variable k1 to be tunable, with storage class ExportedGlobal and type qualifier const. The number of variables and number of specified storage class settings must match. If you specify multiple variables and storage class settings, separate them with a comma.

set_param(gcs,'TunableVars','k1')
set_param(gcs,'TunableVarsStorageClass','ExportedGlobal')
set_param(gcs,'TunableVarsTypeQualifier','const')