Main Content

Edit and Manage Workspace Variables by Using Model Explorer

To learn all of the techniques you can use to create, edit, and manage workspace variables, seeCreate, Edit, and Manage Workspace Variables.

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 theContentspane, right-click the block for which you want to find the variables that it uses.

  2. Select theFind Referenced Variablesmenu 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 theUpdate diagramfield toyesand repeat the search.

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

  • In the Model Explorer, in theModel Hierarchypane, right-click a block or model node and select theFind Referenced Variablesmenu item.

  • In the Model Explorer, in the search bar, use thefor Referenced Variablessearch type option.

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

The金宝appSimulink.findVarsfunction 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金宝appSimulink.findVarsdocumentation.

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, seeFinding Blocks That Use a Specific Variable). Also, you can export variables from the returned set of variables. For details, seeExport 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 modelf14.

  2. Open Model Explorer.

  3. In theModel Hierarchypane, select theBase Workspacenode.

  4. In theContentspane, right-click the variableMqand selectFind Where Used.

  5. In theSelect a systemdialog box, selectf14.

  6. Clear theSearch in referenced modelscheck box, sincef14does not reference any models, and clickOK.

    WithSearch in referenced modelsselected, 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.

    TheUpdate diagram to include recent changescheck 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. ClickOKin response to the message to update the model diagram.

    Because you just opened the model, you must update the model diagram at least once before finding a variable. You could have selectedUpdate diagram to include recent changesin theSelect a systemdialog box to force an initial diagram update, though you typically use that option when you make changes to the model while performing multiple searches withFind Where Used.

  8. Model Explorer displays the search results:

    The property columns whose values includeMqrepresent the block parameters that use theMqvariable. 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 thefor Variable Usagesearch type option.

  • In theSearch Resultspane, right-click a variable and select theFind Where Usedmenu item.

  • In the Model Data Editor, right-click a workspace variable and select theFind Where Usedmenu 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 theModel Hierarchypane and select theFind Unused Variablesmenu item. For example:

  1. Open thef14model.

  2. Open the Model Explorer.

  3. In the search toolbar, set theUpdate diagramfield toyes.

  4. In theModel Hierarchypane, right-click theBase Workspacenode and select theFind Unused Variablesmenu item.

  5. The Model Explorer displays output similar to this:

    The金宝appSimulink.findVarsfunction 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, you can use the Variable Editor to edit variables from the MATLAB®base workspace or model workspace. The Variable Editor is available for editing large arrays and structures.

To open the Variable Editor:

  1. In theContentspane, select the variable.

  2. In the Dialog pane (the right pane), click the buttonnear the value of the variable.

  3. In the menu, selectOpen Variable Editor.

Alternatively, to open the Variable Editor from theContentspane instead of the Dialog pane, begin editing the value of the variable by clicking the appropriate cell. The button appears in the cell.

Representation of Arrays with Three or More Dimensions

When the value of a variable orSimulink.Parameterobject is an array with three or more dimensions, theValuecolumn displays the array as an expression that contains a call to thereshapefunction.

To edit the values in the array, modify the first argument of thereshapecall, which contains all of the array values in a serialized vector. When you add or remove elements along a dimension, you must also correct the argument that represents the length of the modified dimension.

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 modelsldemo_absbrake. The model loads data to the MATLAB base workspace.

  2. Open Model Explorer.

  3. In theModel Hierarchypane, select the base workspace.

  4. In theContentspane, right-click the base workspace variablemand selectRename All.

  5. In theSelect a systemdialog box, click the name of the modelsldemo_absbraketo select it as the context for renaming the variablem.

  6. Clear theSearch in referenced modelscheck box and clickOK. The modelsldemo_absbrakereferences the modelsldemo_wheelspeed_absbrake, but onlysldemo_absbrakeuses the variablem.

    WithSearch in referenced modelsselected, 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.

    TheUpdate diagram to include recent changescheck 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. ClickOKin response to the message to update the model diagram.

    因为你只是打开模型时,您必须更新the model diagram at least once before renaming a variable. You could have selectedUpdate diagram to include recent changesin theSelect a systemdialog 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 theRename Alldialog box, type a new name for the variable in theNew namebox and clickOK.

    You can use the hyperlinks in theCorresponding blockssection of theRename Alldialog box to view the target blocks.

Note

You can rename only variables that the function金宝appSimulink.findVarssupports.

For help with renaming files, use a project. SeeAutomatic Updates When Renaming, Deleting, or Removing Files.

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 selectCompare Selected.

  3. Review the differences in theComparison 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 theFind Referenced Variables选择或金宝appSimulink.findVarsfunction. For details, seeFinding Variables That Are Used by a Model or Block.

Note

所有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 theExportmenu item. For example:

    2. To select individual variables, in theContentspane, select the variables that you want to export. Right-click one of the highlighted variables and select theExport Selectedmenu item.

    If theContentspane 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, seeModel Explorer.

  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.matinstead 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.

To permanently store workspace variables for a model, instead of using the base workspace, create a data dictionary. SeeWhat Is a Data Dictionary?.

Importing Workspace Variables

你可以导入(负载)fi的一组变量le 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 theModel Hierarchypane, right-click the workspace into which you want to import variables.

  2. Select theImportmenu 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.

See Also

|

Related Topics