Create, Edit, and Manage Workspace Variables - MATLAB & Simulink (original) (raw)

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 Simulink.Bus object to explicitly define the structure of a bus.

You can store workspace variables in the base workspace, model workspaces, MAT files, 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, evaluate, and migrate workspace variables:

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.

  1. Open the model f14. The model loads variables into the base workspace.
  2. In the model, open the Property Inspector. On theModeling tab, under Design, click Property Inspector.
  3. In the model, select the Gain block that uses the variableMw.
  4. In the Property Inspector, click the button next to the value of theGain parameter. SelectOpen.
  5. In the Data properties dialog box, type a new value for the variable in the Value box and clickOK.

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:

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:

Analyze Variable Usage in a Model

To analyze the ways in which a model uses variables, use the Model Explorer. You can:

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.

  1. Open the model f14. The model loads variables into the base workspace.
  2. In the model, on the Modeling tab, clickModel Data Editor. In the Model Data Editor, inspect the Parameters tab.
  3. In the model, click the Gain block labeled Mw.
    In the Model Data Editor, the Value column shows that the block uses the variable Mw. Suppose you want to rename this variable.
  4. 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.
  5. Activate the Change scope button.
    Now, the data table shows information about data items in subsystems.
  6. In the Filter contents box, enterMw.
    The data table shows rows that correspond to the variable and to blocks that use the variable.
  7. In the row that represents Mw, right-click and selectRename All.
  8. In the Select a system dialog box, click the name of the model f14 to select it as the context for renaming the variable Mw. 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.
  9. In the Rename All dialog box, type the new name for the variable in the New name box and clickOK.
  10. 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.

Note

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 or data dictionary, you can use the Simulink.data.connect function to create a connection to your data source, then use the common set of functions provided by the Simulink.data.DataConnection object. For data source-specific tasks, the table shows the interfaces and techniques that you can use to programmatically manage variables.

To programmatically list the variables that a model uses or does not use, seeSimulink.findVars.

To programmatically access variables for the purpose of sweeping block parameter values, consider using Simulink.SimulationInput objects instead of modifying the variables through the programmatic workspace interfaces. See Optimize, Estimate, and Sweep Block Parameter Values.

See Also

Topics