Create, Edit, and Manage Workspace Variables
To share information such as parameter values and signal data types between separate blocks
and models, you use workspace variables. For example, you can create a numeric
MATLAB® variable in the base workspace and use the variable to set the value of
the Gain parameter in multiple Gain blocks
simultaneously (see Share and Reuse Block Parameter Values by Creating Variables). You can create a
object to explicitly define the structure of a bus signal.
You can store workspace variables in the base workspace, model workspaces, or data dictionaries. To decide where to store variables, see Determine Where to Store Variables and Objects for Simulink Models.
Tools for Managing Variables
Use one or more of these techniques to create, modify, store, and migrate workspace variables:
To share block parameter values and create
Simulink.Signalobjects (for example, in preparation for code generation), you can use the Model Data Editor. You can interact with all of the block parameters, signal lines, and block states in a model at once. You can also inspect tunable block parameters in a list that you can search, sort, and filter.
To create a variable, in the data table, begin editing the cell that corresponds to a block parameter value (in the Value column) or a signal or state name (in the Name column). Enter the name of the variable you want to create and click the action button in the right side of the cell.
If a block parameter value is already set to a simple numeric expression, you can create a variable for that expression. Click in the right side of the cell that corresponds to the value, then select Create variable. In the Create New Data dialog box, set the name and location for the new variable, then click Create. The cell now displays the new variable.
To modify variables by using the columns in the data table, click the Show/refresh additional information button. Then, the data table contains rows that correspond to the variables and objects that the model uses.
To interact with one variable at a time (for example, to inspect all of the variable properties at once), open the Property Inspector (on the Modeling tab, under Design, click Property Inspector) and select the relevant row in the data table. The Property Inspector shows the properties of the selected variable.
For more information, see Model Data Editor.
To interact with a small number of parameters, signals, or states at a time, use individual block parameter dialog boxes or the Property Inspector to create variables for sharing block parameter values and create and configure parameter and signal objects for code generation.
In the dialog box or the Property Inspector, click the action button next to the value of a block parameter, signal name, or state name.
To create and edit any type or class of variable or object, move variables between workspaces, and inspect all of the variables in a workspace at once, use the Model Explorer. You can also rename variables and precisely analyze the way that an entire model or an individual block uses variables. See Model Explorer and Edit and Manage Workspace Variables by Using Model Explorer.
Edit Variable Value or Property From Block Parameter
This example shows how to change the value of a Gain parameter (Gain block) whose value is set by a numeric variable. Modify the variable, not the block parameter.
Open the model
f14. The model loads variables into the base workspace.
In the model, open the Property Inspector. On the Modeling tab, under Design, click Property Inspector.
In the model, select the Gain block that uses the variable
In the Property Inspector, click the button next to the value of the Gain parameter. Select Open.
In the Data properties dialog box, type a new value for the variable in the Value box and click OK.
Modify Structure and Array Variables Interactively
To inspect and modify a variable whose value is a structure or array, you can launch the Variable Editor by clicking the nearby button . Choose one of these techniques:
In the Model Explorer, select the variable in the Contents pane. In the Dialog pane (the right pane), the button appears.
In the Model Data Editor (on the Modeling tab, click Model Data Editor), on the Parameters tab, click the Show/refresh additional information button. In the data table, find the row that corresponds to the variable and, in the Value column, begin editing the value of the variable. The button appears in the right side of the cell.
In a block dialog box or the Property Inspector, the button appears next to the value of a block parameter that uses the variable. Click the button and use the menu options to open the property dialog box for the variable. Then, in the property dialog box, click the button again to launch the Variable Editor. You can use this technique only for parameter objects such as
Ramifications of Modifying or Deleting a Variable
When you modify or delete a variable, the change can impact multiple blocks and models that use the variable. To assess the impact by determining where the variable is used, use the Model Explorer (see Analyze Variable Usage in a Model). However, you can analyze variable usage only for models that are open at the time of the analysis. Before you perform the analysis, open any models that you suspect use the variable.
Models and blocks use variables through name resolution (see Symbol Resolution). When you change the name of a variable without making corresponding changes to dependent blocks and models, the blocks and models generate errors. Instead, to rename a variable in the context of one or more models, see Rename a Variable Throughout a Model.
When a block or model cannot access a variable that it needs, it generates an error in the Diagnostic Viewer. In some cases, you can use buttons in the Diagnostic Viewer to fix the error (for example, by restoring a deleted variable). To increase the likelihood that you can use the Diagnostic Viewer to recover from the absence of a variable, use these techniques:
Store variables in a data dictionary instead of the base workspace. With a data dictionary, you gain additional options for recovery. For information about data dictionaries, see What Is a Data Dictionary?.
For every model, keep the corresponding Simulink® cache file available. For example, when you share the model with someone else, share the cache file, too. When you fetch the latest model design files from a source control system, fetch the cache file from the continuous integration system or latest build folder. The cache file preserves information that Simulink Coder™ can use to help you recover from the absence of a variable. For more information about Simulink cache files, see Share Simulink Cache Files for Faster Simulation.
Analyze Variable Usage in a Model
To analyze the ways in which a model uses variables, use the Model Explorer. You can:
Determine where a variable is used in a model.
Determine whether a model uses a variable.
Determine which variables in a workspace are not used by a model.
For more information, see Edit and Manage Workspace Variables by Using Model Explorer.
Rename a Variable Throughout a Model
This example shows how to rename a variable in the Model Data Editor.
Open the model
f14. The model loads variables into the base workspace.
In the model, on the Modeling tab, click Model Data Editor. In the Model Data Editor, inspect the Parameters tab.
In the model, click the Gain block labeled
In the Model Data Editor, the Value column shows that the block uses the variable
Mw. Suppose you want to rename this variable.
In the Model Data Editor, click the Show/refresh additional information button.
Now, the data table contains rows that correspond to workspace variables that the model uses.
Activate the Change scope button.
Now, the data table shows information about data items in subsystems.
In the Filter contents box, enter
The data table shows rows that correspond to the variable and to blocks that use the variable.
In the row that represents
Mw, right-click and select Rename All.
In the Select a system dialog box, click the name of the model
f14to select it as the context for renaming the variable
Clear the Search in referenced models check box, since
f14does not reference any models, and click OK.
With Search in referenced models selected, you can rename the target variable everywhere it is used in a model reference hierarchy. However, renaming the target variable in an entire hierarchy can take more time.
The Update diagram to include recent changes check box is cleared by default to save time by avoiding unnecessary model diagram updates. Select the check box to incorporate recent changes you made to the model by forcing a diagram update.
In the Rename All dialog box, type the new name for the variable in the New name box and click OK.
Click Show/refresh additional information again. Because the renaming operation changed the name of the variable and the values of some block parameters, for more accurate information in the Model Data Editor, you must click Show/refresh additional information.
Interact With Variables Programmatically
At the command prompt, you can create and modify variables in the base workspace
by entering commands such as
myVar = 15;. To programmatically
create, modify, and store variables in a different workspace, such as a model
workspace, use the programmatic interface for the target workspace. The table shows
the interfaces and techniques that you can use to programmatically manage
|Target Workspace||Technique or Interface|
|Base workspace||Enter commands at the command prompt.|
|Model workspace||See |
|Data dictionary||See Store Data in Dictionary Programmatically.|
To programmatically list the variables that a model uses or does not use, see
To programmatically access variables for the purpose of sweeping block parameter
values, consider using
objects instead of modifying the variables through the programmatic workspace
interfaces. See Optimize, Estimate, and Sweep Block Parameter Values.