Documentation

Workspace Variables in Model Explorer

Finding Variables That Are Used by a Model or Block

In the Model Explorer, you can get a list of variables that a model or block uses. The following approach is one way to get that list of variables:

  1. In the Contents pane, right-click the block for which you want to find the variables that it uses.

  2. Select the Find Referenced Variables menu item.

Model Explorer returns results similar to these:

For performance, Model Explorer uses cached information from the last compiled version of the model. If you want to recompile the model, either do so manually or, in the Model Explorer, set the Update diagram field to yes and repeat the search.

You can also use the following approaches to find variables that a model or block uses:

  • In the Model Explorer, in the Model Hierarchy pane, right-click a block or model node and select the Find Referenced Variables menu item.

  • In the Model Explorer, in the search bar, use the for Referenced Variables search type option.

  • In the Simulink® Editor, right-click a block, subsystem, or in the canvas and select the Find Referenced Variables menu item. Clicking the canvas returns results for the whole model.

The Simulink.findVars function provides additional options for returning information about workspace variables that is not available from the Model Explorer or Simulink Editor.

For information about limitations when finding referenced variables, see the Simulink.findVars documentation.

Using the Set of Returned Variables

For a variable in the set of returned variables, you can find the blocks that use that variable (for details, see Finding Blocks That Use a Specific Variable). Also, you can export variables from the returned set of variables. For details, see Export Workspace Variables.

Finding Blocks That Use a Specific Variable

This example shows how to use Model Explorer to get a list of blocks that use a specific workspace variable.

  1. Open the model f14.

  2. Open Model Explorer.

  3. In the Model Hierarchy pane, select the Base Workspace node.

  4. In the Contents pane, right-click the variable Mq and select Find Where Used.

  5. In the Select a system dialog box, select f14.

  6. Clear the Search in referenced models check box, since f14 does not reference any models, and click OK.

    With Search in referenced models selected, you can find the target variable everywhere it is used in a model reference hierarchy. However, finding 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.

  7. Click OK in response to the message to update the model diagram.

    Since you just opened the model, you must update the model diagram at least once before finding a variable. You could have selected Update diagram to include recent changes in the Select a system dialog box to force an initial diagram update, though you typically use that option when you make changes to the model while performing multiple searches with Find Where Used.

  8. Model Explorer displays the search results:

    The property columns whose values include Mq represent the block parameters that use the Mq variable. If those property columns are not already in the view, then the Model Explorer adds them to the end of the search results display.

You can also find blocks that use a specific variable by using one of these approaches:

  • In the search bar, select the for Variable Usage search type option.

  • In the Search Results pane, right-click a variable and select the Find Where Used menu item.

Finding Unused Workspace Variables

You can use the Model Explorer to get a list of variables that are defined in a workspace but not used by a model or block. One way to get that list of variables is to right-click a workspace name in the Model Hierarchy pane and select the Find Unused Variables menu item. For example:

  1. Open the f14 model.

  2. Open the Model Explorer.

  3. In the search toolbar, set the Update diagram field to yes.

  4. In the Model Hierarchy pane, right-click the Base Workspace node and select the Find Unused Variables menu item.

  5. The Model Explorer displays output similar to this:

    The Simulink.findVars function provides additional options for returning information about unused workspace variables that is not available from the Model Explorer or Simulink Editor.

Editing Workspace Variables

In the Model Explorer Contents pane, you can use the Variable Editor to edit variables from the MATLAB® workspace or model workspace. The Variable Editor is available for editing large arrays and structures.

To open the Variable Editor for a variable that is an array or structure:

  1. Click the Value cell for the variable.

  2. Select the Variable Editor icon.

The Variable Editor opens:

Right-click in an edit box to open a context menu with several editing options:

You can resize and move the Variable Editor. The Contents pane reflects the edits that you make in the Variable Editor.

When you finish editing, the Variable Editor closes when you click the Close button in the upper right corner of the editor or when you click outside of the editor.

Rename Variables

This example shows how to use Model Explorer to rename a variable everywhere it is used by blocks in Simulink models.

  1. Open the model sldemo_absbrake. The model loads data to the MATLAB base workspace.

  2. Open Model Explorer.

  3. In the Model Hierarchy pane, select the base workspace.

  4. In the Contents pane, right-click the base workspace variable m and select Rename All.

  5. In the Select a system dialog box, click the name of the model sldemo_absbrake to select it as the context for renaming the variable m.

  6. Clear the Search in referenced models check box and click OK. The model sldemo_absbrake references the model sldemo_wheelspeed_absbrake, but only sldemo_absbrake uses the variable m.

    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.

  7. Click OK in response to the message to update the model diagram.

    Since you just opened the model, you must update the model diagram at least once before renaming a variable. You could have selected Update diagram to include recent changes in the Select a system dialog box to force an initial diagram update, though you typically use that option when you make changes to the model while performing multiple variable renaming operations.

  8. In the Rename All dialog box, type a new name for the variable in the New name box and click OK.

    You can use the hyperlinks in the Corresponding blocks section of the Rename All dialog box to view the target blocks.

    Note:   You can rename only variables that the function Simulink.findVars supports.

Compare Duplicate Workspace Variables

You can compare duplicate variables that are stored in the same workspace or in different workspaces. For example, you can compare a variable stored in the base workspace with its duplicate, which is stored in the model workspace.

  1. Open a model and the Model Explorer.

  2. In the search toolbar, search for the variable that is duplicated. Select the rows with the duplicate entries. Then, right-click and select Compare Selected.

  3. Review the differences in the Comparison Viewer.

Export Workspace Variables

You can export (save) a set of variables listed in the Model Explorer, exporting either individual variables or all the variables in the base or model workspace.

One possible workflow is to export the set of variables returned with the Find Referenced Variables option or the Simulink.findVars function. For details, see Finding Variables That Are Used by a Model or Block.

    Note:   All the variables that you export must be from the same workspace.

To export all the variables in a workspace in the Model Explorer to a MATLAB code file or MAT-file:

  1. Select the variables that you want to export.

    1. To select all the variables in a workspace, right-click the workspace node (for example, Base Workspace) and select the Export menu item. For example:

    2. To select individual variables, in the Contents pane, select the variables that you want to export. Right-click one of the highlighted variables and select the Export Selected menu item.

    If the Contents pane has data grouped by a property, selecting the top line in a group does not select all the variables in that group. For details about grouped data, seeGrouping by a Property.

  2. Specify whether to save the variables in a MATLAB code file or a MAT-file.

    The MATLAB code file format is easier to read, is editable, and supports version control. The MAT-file format is binary, which has performance advantages.

    If you specify a MATLAB code file format, the Model Explorer may create an associated MAT-file, reflecting the name of the MATLAB code file, but with an extension of .mat instead of .m.

  3. Specify a name and location for the file.

  4. If the file already exists, Model Explorer displays a dialog box asking you to choose one of these options:

    • Overwrite entire file

      • Replaces all variables in the target file with the selected variables, which are stored in alphabetical order.

    • Update variables that exist in file and append new variables to file

      • Updates existing variables in place and appends new variables.

    • Only update variables that exist in file

      • Updates existing variables, but does not add any new variables, which eliminates potentially extraneous variables.

Importing Workspace Variables

You can import (load) a set of variables from a file into the base workspace or into a model workspace using the Model Explorer. When you import variables into a workspace, the Model Explorer overwrites existing variables and adds any new variables.

To import variables into a workspace:

  1. In the Model Hierarchy pane, right-click the workspace into which you want to import variables.

  2. Select the Import menu item.

  3. In the Import from File dialog box, select a MATLAB code file or MAT-file for the variables that you want to import.

      Note:   If you import a MATLAB code file, then Simulink also imports the associated MAT-file.

Was this topic helpful?