Documentation

Share and Reuse Block Parameter Values by Creating Variables

To set a block parameter value, such as the Gain parameter of a Gain block, you can use numeric variables that you create and store in workspaces such as the base workspace, a model workspace, or a Simulink® data dictionary. You can use the variable to set multiple parameter values in multiple blocks, including blocks in different models. To change the values of the block parameters, you change the value of the variable in the workspace.

Using a variable to set a block parameter value also enables you to:

  • Change the parameter value without having to modify the model file (if you store the variable outside the model workspace).

  • Identify the parameter by a specific, meaningful name when sweeping or tuning the value.

For basic information about setting block parameter values, see Set Block Parameter Values.

Reuse Parameter Values in Multiple Blocks and Models

You can create a numeric MATLAB® variable in a workspace, such as the base workspace or a data dictionary, and use it to specify one or more block parameter values.

The example model sldemo_fuelsys represents the fueling system of a gasoline engine. A subsystem in the model, feedforward_fuel_rate, calculates the fuel demand of the engine by using the constant number 14.6, which represents the ideal (stoichiometric) ratio of air to fuel that the engine consumes. Two blocks in the subsystem use the number to set the values of parameters. In this example, to share the number between the blocks, you create a variable named myParam.

  1. Open the model.

    sldemo_fuelsys

  2. In the model, select View > Model Data. In the Model Data Editor, inspect the Parameters tab.

  3. In the model, navigate into the subsystem.

    open_system(...
        'sldemo_fuelsys/fuel_rate_control/fuel_calc/feedforward_fuel_rate')

  4. In the Model Data Editor, in the Filter contents box, enter 14.6.

    The data table contains two rows, which correspond to the Constant value parameters of two of the Constant blocks in the subsystem.

  5. Use the Value column to replace the literal number 14.6 with myParam. Perform the replacement for both parameters.

  6. In the Filter contents box, enter myParam.

  7. While editing the value of one of the parameters, click the action button and select Create.

  8. In the Create New Data dialog box, set Value to 14.6 and click Create.

    The variable, myParam, appears in the base workspace.

Because the variable exists in the base workspace, you can use it in multiple models. However, when you end your MATLAB session, you lose the contents of the base workspace. Consider permanently storing the variable in a model workspace or data dictionary.

Define a System Constant

To define a system constant, such as a variable that represents the radius of the Earth, consider creating a Simulink.Parameter object instead of a numeric MATLAB variable. Parameter objects allow you to specify physical units and custom documentation as well as other characteristics.

To create and use parameter objects in models, see Data Objects.

Control Scope of Parameter Values

The scope of a variable is the set of models and blocks that can use the variable. For example, variables that you create in the base workspace have global scope because all blocks in all open models can use the variables. Variables that you store in a model workspace have limited scope because only the blocks in the host model can use the variables.

You cannot create two variables that have the same name in the same scope. Controlling the scope of a variable helps you to avoid name conflicts and establish clear ownership of the variable.

The table describes the different ways that you can control the scope of a reusable parameter value.

ScopeTechnique
All open modelsCreate a variable in the base workspace.
One or more targeted modelsCreate a variable in a data dictionary. To reuse the variable in multiple models, create a referenced dictionary. See What Is a Data Dictionary?
One model, including all subsystems in the modelCreate a variable in the model workspace. See Model Workspaces.
Multiple blocks inside a subsystem, including blocks in nested subsystems

Mask the subsystem and create a mask parameter instead of a workspace variable.

To prevent blocks inside a subsystem from using workspace variables, in the subsystem block dialog box, set Permit Hierarchical Resolution to None. This technique allows you to use the same name to create both a variable in a workspace and a mask parameter in the subsystem mask. The blocks in the subsystem can use only the mask parameter.

For information about subsystems, see Subsystem. For information about masking, see Masking Fundamentals.

To avoid name conflicts when you have a large model with many variables in the same scope, consider packaging the variables into a single structure. For more information, see Organize Related Block Parameter Definitions in Structures.

For basic information about how blocks use the variable names that you specify, see Symbol Resolution.

Permanently Store Workspace Variables

Variables that you create in the base workspace do not persist between MATLAB sessions. However, you can store the variables in a MAT-file or script file, and load the file whenever you open the model using a model callback. A model callback is a set of commands that Simulink executes when you interact with a model in a particular way, such as opening the model. You can use a callback to load variables when you open the model. Use this technique to store variables while you learn about Simulink and experiment with models.

  1. In a model that contains a Gain block, set the value of the Gain parameter to K.

  2. At the command prompt, create a variable K in the base workspace.

    K = 27;

  3. In the Workspace browser, right-click the variable and select Save As.

    To save multiple variables in one file, select all of the target variables in the Workspace browser, and then right-click any of the selected variables.

  4. In the dialog box, set Save as type to MATLAB Script. Set File name to loadvar and click Save.

    The script file loadvar.m appears in your current folder. You can open the file to view the command that creates the variable K.

  5. In the model, select File > Model Properties > Model Properties.

  6. In the Callbacks tab of the Model Properties dialog box, select PreLoadFcn as the callback that you want to define. In the Model pre-load function pane, enter loadvar and click OK.

  7. Save the model.

The next time that you open the model, the PreloadFcn callback loads the variable K into the base workspace. You can also save the variable to a MAT-file, for example loadvar.mat, and set the model callback to load loadvar.

To learn about callbacks, see Callbacks for Customized Model Behavior and Callbacks for Customized Model Behavior. To programmatically define a callback for loading variables, see Programmatically Store Workspace Variables for a Model.

When you save variables to a file, you must save the changes that you make to the variables during your MATLAB session. To permanently store variables for a model, consider using a model workspace or a data dictionary instead of a MAT-file or script file. For more information about permanently storing variables, see Determine Where to Store Variables and Objects for Simulink Models.

Programmatically Store Workspace Variables for a Model

In the example above, you define a model callback that creates variables when you open a model. You can programmatically save the variable and set the model callback.

  1. At the command prompt, create the variable K in the base workspace.

    K = 27;
  2. Save the variable to a new script file named loadvar.m.

    matlab.io.saveVariablesToScript('loadvar.m','K')

  3. Set the model callback to load the script file.

    set_param('mymodel','PreloadFcn','loadvar')

  4. Save the model.

    save_system('myModel')

The function matlab.io.saveVariablesToScript saves variables to a script file. To save variables to a MAT-file, use the function save. To programmatically set model properties such as callbacks, use the function set_param.

Manage and Edit Workspace Variables

When you use variables to set block parameter values, you store the variables in a workspace or data dictionary. You can use the command prompt, the Model Explorer, and the Model Data Editor to create, move, copy, and edit variables. You can also determine where a variable is used in a model, list all of the variables that a model uses, and list all of the variables that a model does not use. For more information, see Create, Edit, and Manage Workspace Variables.

Package Shared Breakpoint and Table Data for Lookup Tables

To share breakpoint vectors or table data between multiple n-D Lookup Table, Prelookup, and Interpolation Using Prelookup blocks, consider storing the data in Simulink.LookupTable and Simulink.Breakpoint objects instead of MATLAB variables or Simulink.Parameter objects. This technique improves model readability by clearly identifying the data as parts of a lookup table and explicitly associating breakpoint data with table data.

Store Standalone Lookup Table in Simulink.LookupTable Object

A standalone lookup table consists of a set of table data and one or more breakpoint vectors. You do not share the table data or any of the breakpoint vectors with other lookup tables.

When you share a standalone lookup table, you use all of the table and breakpoint data together in multiple n-D Lookup Table blocks. To store this data in a Simulink.LookupTable object:

  1. Create the object in a workspace or data dictionary. For example, at the command prompt, enter:

    myLUTObj = Simulink.LookupTable;

  2. Use the properties of the object to store the values of the table and breakpoint data.

  3. Use the properties of the object to configure a unique name for the structure type in the generated code. In the property dialog box, under Struct Type definition, specify Name.

  4. In the n-D Lookup Table blocks, set Data specification to Lookup table object.

  5. To the right of Data specification, set Name to the name of the Simulink.LookupTable object.

For ways to create and configure Simulink.LookupTable objects, see Simulink.LookupTable

Store Shared Data in Simulink.LookupTable and Simulink.Breakpoint Objects

When you use Prelookup and Interpolation Using Prelookup blocks to more finely control the lookup algorithm, you can share breakpoint vectors and sets of table data. For example, you can share a breakpoint vector between two separate sets of table data. With this separation of the breakpoint data from the table data, you can share individual parts of a lookup table instead of sharing the entire lookup table.

To store breakpoint and table data:

  1. Create a Simulink.LookupTable object for each unique set of table data. Create a Simulink.Breakpoint object for each unique breakpoint vector, including breakpoint vectors that you do not intend to share.

  2. Use the properties of the objects to store the values of the table and breakpoint data.

  3. Configure the Simulink.LookupTable objects to refer to the Simulink.Breakpoint objects for breakpoint data. In the Simulink.LookupTable objects, set Specification to Reference. Specify the names of the Simulink.Breakpoint objects.

  4. In the Interpolation Using Prelookup blocks, set Specification to Lookup table object. Set Name to the name of a Simulink.LookupTable object.

    In the Prelookup blocks, set Specification to Breakpoint object. Set Name to the name of a Simulink.Breakpoint object.

The example model fxpdemo_lookup_shared_param contains two Prelookup and two Interpolation Using Prelookup blocks. Configure the blocks so that each combination of a Prelookup and an Interpolation Using Prelookup block represents a unique lookup table. Share the breakpoint vector between the two lookup tables. In this case, each lookup table has unique table data but shared breakpoint data.

  1. Open the example model.

  2. In the Prelookup block dialog box, set Specification to Breakpoint object. Set Name to sharedBkpts.

  3. Click the button next to the value of the Name parameter. Select Create Variable.

  4. In the Create New Data dialog box, set Value to Simulink.Breakpoint and click Create.

    A Simulink.Breakpoint object appears in the base workspace.

  5. In the property dialog box for sharedBkpts, specify Value as a vector such as [1 2 3 4 5 6 7 8 9 10]. Click OK.

  6. In the Prelookup block dialog box, click OK.

  7. In the Prelookup1 block dialog box, set Specification to Breakpoint object. Set Name to sharedBkpts.

  8. In the Interpolation Using Prelookup block dialog box, set Specification to Lookup table object. Set Name to dataForFirstTable.

  9. Click the button next to the value of the Name parameter. Select Create Variable.

  10. In the Create New Data dialog box, set Value to Simulink.LookupTable and click Create.

    A Simulink.LookupTable object appears in the base workspace.

  11. In the property dialog box for dataForFirstTable, specify Value as a vector, such as [10 9 8 7 6 5 4 3 2 1].

  12. Set Specification to Reference.

  13. In the table under Specification, set Name to sharedBkpts and click OK.

  14. In the Interpolation Using Prelookup block dialog box, click OK.

  15. Configure the Interpolation Using Prelookup1 block to use a Simulink.LookupTable object named dataForSecondTable. In the object property dialog box, specify Value as a vector, such as [0 0.5 1 1.5 2 2.5 3 3.5 4 4.5]. Configure the object to refer to sharedBkpts for the breakpoint data.

The model now represents two unique lookup tables:

  • A combination of sharedBkpts and dataForFirstTable.

  • A combination of sharedBkpts and dataForSecondTable.

These lookup tables share the same breakpoint data through sharedBkpts.

Related Topics

Was this topic helpful?