Main Content

Configure MATLAB Function Block Parameter Variables

You can use parameter variables in MATLAB Function blocks to access the same data in multiple instances of a MATLAB Function block. You can assign parameter variables to data in a workspace or create a block mask and assign the variable to a mask parameter.

Assign Parameter Variables to Workspace Data

MATLAB Function block parameter variables get their value from a workspace variable or a Simulink.Parameter object. Before you create a parameter variable, you must create the workspace variable or Simulink.Parameter object in one of these spaces:

  • The Simulink® model workspace.

  • A data dictionary. For more information, see What Is a Data Dictionary?

  • The MATLAB® workspace. If you close MATLAB, you must redefine the data.

You can create the workspace variable or object directly in the workspace, load the variable in a MAT-file or MATLAB file, or create the variable with custom MATLAB code. This table shows the differences between the different spaces:

Data Storage SpaceVisibility
Model WorkspaceLocal to the model
Data DictionaryShared with specified Simulink models
Base WorkspaceAvailable to all Simulink models.

For more information, see Determine Where to Store Variables and Objects for Simulink Models.

After you create the workspace variable or object, you can create the parameter variable in the MATLAB Function block:

  1. Open the MATLAB Function block.

  2. In the MATLAB Function Block Editor, in the Function tab, in the Prepare section, click Edit Data.

  3. In the Symbols pane, click the Create Data button .

  4. Select the variable and set the Scope property. In the Type column, click the type icon and select Parameter Data. The Symbols pane indicates the variable has Scope set to Parameter with the Parameter Data icon .

  5. In the Name column, set the name of the parameter variable to the name of the workspace variable or Simulink.Parameter object.

For more information about defining variables in MATLAB Function blocks, see Create and Define MATLAB Function Block Variables.

MATLAB Function Block Parameter Variable Example

This example uses a MATLAB Function block with a parameter variable. In this example, a variable in the base workspace defines the value of the parameter value.

The MATLAB function multiplies the parameter variable const by the value of the Sine Wave block signal. Open the MATLAB Function block to view the code.

function y = fcn(u, const)
y = const*u;

When the model loads, it defines the base workspace variable, const, by running the PreLoadFnc callback. To view the callback, in the Modeling tab, click Model Settings > Model Properties. In the Model Properties window, in the Callbacks tab, click, PreLoadFnc.

You can adjust the variable by redefining it in the MATLAB® Command Window.

In the MATLAB Function block, in the Function tab, click Edit Data. In the Symbols pane, click const. The Property Inspector and Symbols pane show that const is a parameter variable.

The parameter variable and the workspace variable must have the same name. In this example, changing the parameter variable name without changing the workspace variable name causes an error.

Assign Parameter Variables to Mask Parameters

You can assign values to parameter variables in a MATLAB Function block by creating a block mask and setting the value of the parameter in the mask. To assign a parameter variable to a mask parameter:

  1. Open the MATLAB Function block.

  2. In the MATLAB Function Block Editor, in the Function tab, in the Prepare section, click Edit Data.

  3. In the Symbols pane, click the Create Data button .

  4. Select the variable and set the Scope property. In the Type column, click the type icon and select Parameter Data.

  5. In the Name column, set the name of the parameter variable and exit the block.

  6. In the model, mask the MATLAB Function block. For more information about creating masks for blocks, see Masking Fundamentals.

  7. In the Mask Editor window, in the Parameters & Dialog pane, add a parameter and use the same name as the parameter variable.

  8. Save the mask and exit the Mask Editor window.

Masked MATLAB Function Block Parameter Variable Example

This example uses a mask to set the value of a parameter variable.

The MATLAB function multiplies the parameter variable const by the value of the Sine Wave block signal. Click the arrow icon on the MATLAB Function block to view the code.

function y = fcn(u, const)
y = const*u;

You can adjust the value of const by double-clicking the MATLAB Function block.

The block mask defines the Set sine wave multiplier parameter, which you can edit in the Mask Editor. The MATLAB Function block defines const as data with Scope set to Parameter in the Property Inspector. You can see this property in the Property Inspector when you select const in the Symbols pane.

The parameter variable and the mask parameter must have the same name. In this example, changing the parameter variable name without changing the mask parameter name causes an error. If you are using a non-tunable parameter, ensure that the Tunable property is cleared in the Property Inspector.

Parameter Variable Hierarchy

When you define parameter variables, Simulink searches for and selects variable assignments by using the same rules for symbol resolution. See Symbol Resolution. If a variable of the same name exists in more than one of the workspaces visible to the block, the block uses the data closest to the block in the workspace hierarchy. If the MATLAB Function block is in a masked subsystem, the block uses the data in the subsystem workspace. Otherwise, the block uses the data in the model workspace, followed by using the data in the MATLAB base workspace and in an associated Simulink data dictionary, in that order.

See Also

Related Topics