Create and Define MATLAB Function Block Variables - MATLAB & Simulink (original) (raw)
MATLAB Function blocks use variables to manage simulation data. Variables can represent data for block inputs, outputs, parameters, or from Data Store Memory blocks. 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, Symbols pane, or Model Explorer.
Add Variables Directly to Code
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.
You cannot define global variables by only adding them to the code. See Access Data Store Data in MATLAB Function Blocks.
Use the Symbols pane
Since R2022a
To use the Symbols pane, double-click the MATLAB Function block to open the MATLAB Function Block Editor. Then, open the Symbols pane by using one of these approaches:
- In the Function tab, click Edit Data.
- In the Modeling tab, in the section, click Symbols Pane.
Add variables by clicking the Create Data button. Delete variables by right-clicking the variable and clicking .
Use the Model Explorer
To use the Model Explorer, in theModeling tab, in the section, click Model Explorer. In the pane, expand the model tree view and select the MATLAB Function block.
Add variables by clicking > or the Add Data button . Delete variables by selecting the variable and clicking > 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 .
- In the Modeling tab, in the Design Data section, click Property Inspector. In theSymbols 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 theType column of the Symbols pane. You can setScope to one of these values:
Port
Specifies the index of the port associated with the variable. This property applies only to variables with the Scope property set toInput
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 theScope 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, seeInherit 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 toOutput
. 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 propertySupport 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 toParameter
. 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:
Complexity | Description |
---|---|
Inherited | Variable 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. |
Off | Variable is a real number. |
On | Variable 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 toInput
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 theScope property set to Input
orOutput
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 isinf
.
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
orOutput
. 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.
Programmatically Create Variables
You can specify MATLAB Function block variable properties programmatically by retrieving the Stateflow.EMChart object of the block. For more information, see Configure MATLAB Function Blocks Programmatically.