Main Content

Simulink.VariableUsage

Store information about the relationship between variables and blocks in models

Description

A Simulink.VariableUsage object describes where a variable is used in models.

Use this information to:

  • Prepare to permanently store the variables in files and workspaces. For more information about storing variables for a model, see Determine Where to Store Variables and Objects for Simulink Models.

  • Reduce the number of variables that you need to store by eliminating unused variables.

  • Prepare to partition variables and establish variable ownership when you work in a team.

To analyze variable usage in models, use Simulink.VariableUsage objects together with the Simulink.findVars function. The function returns and accepts Simulink.VariableUsage objects as arguments. For more information, see Simulink.findVars.

A Simulink.VariableUsage object can also describe the usage of an enumerated data type.

Only a Simulink.VariableUsage constructor or the Simulink.findVars function can set property values in a Simulink.VariableUsage object. The properties are otherwise read only.

Creation

The Simulink.findVars function returns Simulink.VariableUsage objects.

To create variable usage objects for use as a filter when using Simulink.findVars, use the Simulink.VariableUsage function described below.

Description

variableUsageObj = Simulink.VariableUsage(varNames,sourceName) creates an array of Simulink.VariableUsage objects to describe the variables varNames. The constructor sets the Name property of each object to one of the variable names specified by varNames, and sets the Source property of all the objects to the source specified by sourceName. You can specify varNames with variables that are not used in any loaded models.

example

Input Arguments

expand all

Names of target variables, specified as a character vector or a cell array of character vectors. The constructor creates a Simulink.VariableUsage object for each variable name.

Example: 'k'

Example: {'k','asdf','fuelFlow'}

Data Types: char | cell

Name of the source that defines the target variables, specified as a character vector. For example, you can specify the MATLAB® base workspace or a data dictionary as a source. The constructor also determines and sets the SourceType property of each of the returned Simulink.VariableUsage objects.

Example: 'base workspace'

Example: 'myModel'

Example: 'myDictionary.sldd'

Data Types: char

Properties

expand all

This property is read-only.

The name of the variable or enumerated data type the object describes, returned as a character vector.

This property is read-only.

The name of the workspace or data dictionary that defines the described variable, returned as a character vector. The table shows some examples.

Source ValueMeaning
'base workspace'MATLAB base workspace
'MyModel'Model workspace for the model MyModel
'MyModel/Mask1'Mask workspace for the masked block Mask1 in the model MyModel
'sldemo_fuelsys_dd_controller.sldd'The data dictionary named 'sldemo_fuelsys_dd_controller.sldd'

The table shows some examples if you created the Simulink.VariableUsage object by using the Simulink.findVars function to find enumerated data types.

Source ValueMeaning
'BasicColors.m'The enumerated type is defined in the MATLAB file 'BasicColors.m'.
''The enumerated type is defined dynamically and has no source.
'sldemo_fuelsys_dd_controller.sldd'The enumerated type is defined in the data dictionary named 'sldemo_fuelsys_dd_controller.sldd'.

This property is read-only.

The type of the workspace that defines the variable, returned as a character vector. The possible values are:

  • 'base workspace'

  • 'model workspace'

  • 'mask workspace'

  • 'data dictionary'

If you created the Simulink.VariableUsage object by using the Simulink.findVars function to find enumerated data types, the possible values are:

  • 'MATLAB file'

  • 'dynamic class'

  • 'data dictionary'

This property is read-only.

Blocks that use the variable or models that use the enumerated type, returned as a cell array of character vectors. Each character vector names a block or model that uses the variable or enumerated type. The Simulink.findVars function populates this property.

Object Functions

Examples

collapse all

Return a Simulink.VariableUsage object for a variable k in the base workspace.

var = Simulink.VariableUsage('k','base workspace');

You can use var as a filter for the Simulink.findVars function.

Return an array of Simulink.VariableUsage objects containing one object for each variable returned by the whos command in the base workspace.

vars = Simulink.VariableUsage(whos,'base workspace')

Return an array of Simulink.VariableUsage objects that describes all the variables in a model workspace.

hws = get_param('mymodel','ModelWorkspace');
vars = Simulink.VariableUsage(hws.whos,'MyModel')

Return an array of Simulink.VariableUsage objects that describes all the variables in a mask workspace.

maskVars = get_param('mymodel/maskblock','MaskWSVariables');
vars = Simulink.VariableUsage(maskVars,'mymodel/maskblock');

Version History

Introduced in R2012b