Main Content

Simulink.findVars

Analyze relationship between variables and blocks in models

Description

vars = Simulink.findVars(ctxt) finds and returns variables that are used in the blocks and models specified by context, including subsystems and referenced models. The function returns an empty vector if context does not use any variables.

example

vars = Simulink.findVars(ctxt,varfilter) finds only the variables or enumerated types that are specified by variablefilter. For example, use this syntax to determine where a variable is used in a model.

example

vars = Simulink.findVars(___,Name,Value) finds variables with additional options specified by one or more Name,Value pair arguments. For example, you can search for unused variables. You can also search for enumerated data types that are used in context, in addition to variables.

example

Examples

collapse all

Find variables used by MyModel.

variables = Simulink.findVars('MyModel');

Find all uses of the base workspace variable k by MyModel. Use the cached results to avoid compiling MyModel.

variables = Simulink.findVars('MyModel','Name','k',...
'SearchMethod','cached','SourceType','base workspace');

Find all uses of a variable whose name matches the regular expression ^trans.

variables = Simulink.findVars('MyModel','Regexp','on',...
'Name','^trans');

Given two models, find the variables used by the first model, the second, and both.

model1Vars = Simulink.findVars('model1');
model2Vars = Simulink.findVars('model2');
commonVars = intersect(model1vars,model2Vars);

Find the variables that are defined in the model workspace of MyModel but that are not used by the model.

unusedVars = Simulink.findVars('MyModel','FindUsedVars','off',...
'SourceType','model workspace');

Determine if the base workspace variable k is not used by MyModel.

varObj = Simulink.VariableUsage('k','base workspace');
unusedVar = Simulink.findVars('MyModel',varObj,...
'FindUsedVars','off');

Find the variables that are used by the block Gain1 in MyModel.

variables = Simulink.findVars('MyModel',...
'Users','MyModel/Gain1');

Find the variables that are used in a model reference hierarchy. Begin the search with the model MyNestedModel, and search the entire hierarchy below MyNestedModel.

variables = Simulink.findVars('MyNestedModel','SearchReferencedModels','on');

Find variables and enumerated types that are used in MyModel.

varsAndEnumTypes = Simulink.findVars('MyModel','IncludeEnumTypes','on');

Input Arguments

collapse all

Models and blocks to search, specified as a character vector, string, cell array of character vectors, or a string array. You can specify context in one of the following ways:

  • The name of a model. For example, ('vdp') specifies the model vdp.slx.

  • The name or path of a block or masked block. For example, ('vdp/Gain1') specifies a block named Gain1 at the root level of the model vdp.slx.

  • A cell array of model or block names.

Data Types: char | cell

Specific variables to find, specified as an array of Simulink.VariableUsage objects. Each Simulink.VariableUsage object identifies a variable to find.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'FindUsedVars','off'

Flag to find variables that are explicitly used or not used, specified as the comma-separated pair consisting of 'FindUsedVars' and 'on' or 'off'. If you specify FindUsedVars as 'off', the function finds variables that are not used in context but that are defined in the workspace specified by SourceType.

Example: 'FindUsedVars','off'

Flag to find enumerated data types that are used, specified as the comma-separated pair consisting of 'IncludeEnumTypes' and 'on' or 'off'. The function finds enumerated types that are used explicitly in context as well as types that define variables that are used in context.

The function does not report details about which blocks or objects are using the enumerated types.

If you specify SourceType as 'base workspace', 'model workspace', or 'mask workspace', the function does not report enumerated types because those sources cannot define enumerated types.

You cannot find unused enumerated types by specifying FindUsedVars as 'off'.

Example: 'IncludeEnumTypes','on'

Flag to enable regular expression matching for input arguments, specified as the comma-separated pair consisting of 'RegExp' and 'on'. You can match only input arguments that have character vector values.

Example: 'RegExp','on'

Compile status, specified as the comma-separated pair consisting of 'SearchMethod' and one of these values:

  • 'compiled' — Return up-to-date results by compiling every model in the search context before search.

  • 'cached' — Return quicker results by using results cached during the previous compile.

Example: 'SearchMethod','compiled'

Flag to enable search in referenced models, specified as the comma-separated pair consisting of 'SearchReferencedModels' and 'on'.

If a referenced model uses a global variable, such as a variable stored in a data dictionary, the function returns this global variable when called on the parent model. If SearchReferencedModels is set to 'on', the Simulink.VariableUsage object returned by the function specifies a block in the referenced model as the variable user. Otherwise, the object specifies the Model block as the user.

Example: 'SearchReferencedModels','on'

Name of a variable or enumerated data type to search for, specified as the comma-separated pair consisting of 'Name' and a character vector or string.

Example: 'Name','trans'

Data Types: char

Workspace or source defining the variables, specified as the comma-separated pair of 'SourceType' and one of these options:

  • 'base workspace'

  • 'model workspace'

  • 'mask workspace'

  • 'data dictionary'

  • 'external source' (since R2024b)

The function filters results for variables that are defined in the specified source.

Example: 'SourceType','base workspace'

If you search for enumerated data types by specifying 'IncludeEnumTypes' as 'on', 'SourceType' represents the way an enumerated type is defined. You can specify one of these options:

  • 'MATLAB file'

  • 'dynamic class'

  • 'data dictionary'

The function filters results for enumerated types that are defined in the specified source.

Example: 'SourceType','MATLAB file'

If you do not specify SourceType, the function does not filter results by source.

Name of specific block to search for variables, specified as the comma-separated pair consisting of 'Users' and a character vector or string.

To search a set of specific blocks, enable regular expression matching by specifying RegExp as 'on' and use regular expressions in the character vector. For example, you can specify 'Users','MyModel/Gain*' to search all blocks in MyModel whose names begin with Gain.

Example: 'Users','MyModel/Gain1'

Example: 'Users','MyModel/mySubsystem/Gain2'

Example: 'Users','MyModel/Gain*'

Limitations

Simulink.findVars does not work with these constructs:

  • MATLAB® code in scripts and initialization and callback functions.

  • Libraries and blocks in libraries.

  • Variables in MATLAB Function blocks, except for input arguments. However, Simulink.findVars can find enumerated types anywhere they are used in MATLAB Function blocks.

  • Calls directly to MATLAB from a Stateflow® block.

  • S-functions that use data type variables registered by using ssRegisterDataType. To make the variables searchable, use ssRegisterTypeFromNamedObject instead.

  • Variables inside a string.

  • Fields in a structure.

  • When 'SourceType' is specified as 'external source', Simulink.findVars does not identify unused variables.

  • Top models configured for parallel model reference builds. In this case, Simulink.findVars does not work when called with either of these name-value arguments:

    • 'SearchMethod' specified as 'cached'

    • 'SearchReferencedModels' specified as 'on'

Note

Simulink.findVars does not discover variable usage in inactive Variant Subsystem blocks or inactive paths of Variant Source or Variant Sink blocks if the Variant activation time parameter is set to update diagram.

Version History

Introduced in R2010a