Main Content

Create and Define MATLAB Function Block Variables

MATLAB Function blocks use variables to manage simulation data. Variables can represent data for block inputs, outputs, parameters, or from Data Store Memory. You can create or delete variables in the MATLAB Function block code, the Symbols pane, or the Model Explorer. Then you can set the properties of the variables in the Property Inspector or Model Explorer.

Create and Delete MATLAB Function Block Variables

You can create or delete MATLAB Function block variables in the function code, by using the Symbols pane, or Model Explorer.

To create or delete variables in the MATLAB Function block code, double-click the MATLAB Function block to open the MATLAB Function Block Editor. When you specify new input and output arguments, the MATLAB Function block automatically assigns the arguments to the respective input or output port. Similarly, you can remove arguments from the block by deleting them and updating or saving the model.

To use the Symbols pane, double-click the MATLAB Function block to open the MATLAB Function Block Editor. Then use one of these approaches:

  • In the Function tab, click Edit Data.

  • the Modeling tab, in the Design section, click Symbols Pane.

Add variables by clicking the Create Data button . Delete variables by right-clicking the variable and clicking Delete.

To use the Model Explorer, in the Modeling tab, in the Design section, click Model Explorer. In the Model Hierarchy pane, expand the model tree view and select the MATLAB Function block. Add variables by clicking Add > Data or the Add Data button . Delete variables by selecting the variable and clicking Edit > Delete or the Delete button .

If you do not use the variable in the code, the Symbols pane shows the Unused Symbol icon next to the variable name.

Set General Variable Properties

You can modify the properties of a variable by using the Symbols pane and the Property Inspector or by using the Model Explorer.

To edit the properties by using the Symbols pane and Property Inspector, use of one of these approaches:

  • In the Function tab, click Edit Data. In the Symbols pane, select the variable.

  • In the Symbols pane, right-click a variable name and click Inspect.

  • In the Modeling tab, in the Design Data section, click Property Inspector. In the Symbols pane, select the variable.

To edit the properties by using Model Explorer, click a variable, then edit the properties in the General tab.

Name

Specifies the name of the variable. Use the same naming conventions used in MATLAB®. You can also modify this property in the Symbols pane directly.

Changing the name in the Symbols pane only adjusts variables in the function declaration statement, and does not change the name of the variable used throughout the body of the code. If you want to adjust the variable throughout the code, enter the Name property in the Symbols pane and press Shift + Enter.

Scope

Specifies where the variable resides in memory relative to its parent. This property determines the functionality of the variable. You can also modify this property in the Type column of the Symbols pane. You can set Scope to one of these values:

ScopeDescription
ParameterThe variable resides in a variable of the same name in the MATLAB workspace, the model workspace, or in the workspace of a masked subsystem that contains this block. If a variable of the same name exists in more than one of the workspaces visible to the block, the block uses the variable closest to the block in the workspace hierarchy. The Symbols pane indicates the variable has Scope set to Parameter with the Parameter Data icon . For more information, see Model Workspaces.
InputThe variable is an input signal to the MATLAB Function block. The Symbols pane indicates the variable has Scope set to Input with the Input Data icon .
OutputThe variable is an output signal of the MATLAB Function block. The Symbols pane indicates the variable has Scope set to Output with the Output Data icon .
Data Store MemoryThe variable resides in a Data Store Memory block in the model. The Symbols pane indicates the variable has Scope set to Data Store Memory with the Data Store Memory icon . For more information, see Storing Data Using Data Store Memory Blocks.

For more information, see Check Properties for MATLAB Function Block Variables and Configure MATLAB Function Block Parameter Variables.

Port

Specifies the index of the port associated with the variable. This property applies only to variables with the Scope property set to Input or Output.

Data must resolve to signal object

Specifies that the variable resolves to a Simulink® signal object. This property applies only to variables with the Scope property set to Output. This property appears only if you set the configuration parameter Signal resolution to a value other than None. For more information, see Symbol Resolution.

Size

Specifies the size of the variable. This property can be a scalar value or a MATLAB vector of values. Size defaults to –1, which means that the size is inherited. For more information, see Inherit Size from Simulink. This property does not apply to variables with the Scope property set to Data Store Memory. See Specify Size of MATLAB Function Block Variables.

Variable size

Specifies whether the size of the variable can change or is fixed. This property applies only to variables with the Scope property set to Output. For more information, see Declare Variable-Size MATLAB Function Block Variables.

Input variables inherit size variability from their corresponding signals. Variables in both the input and output of the function declaration statement also automatically inherit size variability.

This property appears only if the MATLAB Function block property Support variable-size arrays is enabled. See Support variable-size arrays.

Tunable

Specifies whether the parameter used as the source of this variable data is tunable. For more information, see Tunable Parameters. This property applies only to variables with the Scope property set to Parameter. Clear this option if the parameter must be a constant expression, such as for toolbox functions supported for code generation. For more information, see Functions and Objects Supported for C/C++ Code Generation.

Complexity

Specifies real or complex variables. Set Complexity to one of the following values:

ComplexityDescription
InheritedVariable inherits complexity based on the Scope property. Input and output variables inherit complexity from the Simulink signals connected to the associated input or output ports. Parameter variables inherit complexity from the specified parameter.
OffVariable is a real number.
OnVariable is a complex number.

This property does not apply to variables with the Scope property set to Data Store Memory.

Type

Specifies the data type for the variable. You can specify the data type by:

  • Selecting a built-in type from the Type property.

  • Entering an expression in the Type property that evaluates to a data type. See About Data Types in Simulink.

  • Using the Data Type Assistant to specify the Mode property, then specifying the data type based on that mode.

    Note

    To display the Data Type Assistant, click the Show data type assistant button .

For more information, see Define and Modify Variable Data Types. This property does not apply to variables with the Scope property set to Data Store Memory.

Unit (e.g., m, m/s^2, N*m)

Specifies the physical units for the variable. By default, this property is set to inherit the unit from the Simulink signal on the corresponding input or output port. See Specify Units in MATLAB Function Blocks. The property applies only to variables with the Scope property set to Input or Output.

Set Limit Range Properties

You can set the range of acceptable values for variables in the Limit range section of the Property Inspector or Model Explorer. The MATLAB Function block uses this range to validate the data as it enters or leaves the block. You can enter an expression or parameter that evaluates to a numeric scalar. Specify the range by using two properties. These properties apply only to variables with the Scope property set to Input or Output

Minimum

The smallest value allowed for the variable during simulation. The default value is -inf.

Maximum

The largest value allowed for the variable during simulation. The default value is inf.

Set Description Properties

You can modify the description properties of a variable by using the Property Inspector or the Model Explorer. In the Model Explorer, click a variable, then edit the properties in the Description tab. In the Property Inspector, edit the properties in the Info tab.

Save final value to base workspace

Specifies if the MATLAB Function block assigns the value of the variable to a variable of the same name in the MATLAB base workspace after simulation. This property applies only to variables with the Scope property set to Input or Output. This property appears only in the Model Explorer.

Description

Specifies the description of the variable.

Document link

Specifies the link to the documentation for the variable. You can enter a URL address or a MATLAB command that displays documentation, such as an HTML file or text in the MATLAB Command Window. When you click the Document link link, the MATLAB Function block evaluates the link and displays the documentation.

See Also

Related Topics