Documentation

Simulink.findVars

Find variables in models and blocks

Syntax

  • [variables] = Simulink.findVars(context) example
  • [variables] = Simulink.findVars(context,variablefilter) example
  • [variables] = Simulink.findVars(___,Name,Value) example

Description

example

[variables] = Simulink.findVars(context) 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

[variables] = Simulink.findVars(context,variablefilter) finds only the variables or enumerated types that are specified by variablefilter.

example

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

Examples

collapse all

Variables in Use in a Model

Find variables used by MyModel.

variables = Simulink.findVars('MyModel');

Specific Variable in Use in a Model

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');

Regular Expression Matching

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

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

Variables Common to Two Models

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);

Variables Not Used in a Model

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',false,
'SourceType','model workspace');

Specific Variable Not Used in a Model

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

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

Variables Used by a Block

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

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

Variables Used in a Model Reference Hierarchy

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');

Variables and Enumerated Types Used in a Model

Find variables and enumerated types that are used in MyModel.

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

Related Examples

Input Arguments

collapse all

context — Models and blocks to searchstring | cell array of strings

Models and blocks to search, specified as a string or a cell array of strings. 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

variablefilter — Specific variables to findarray of Simulink.VariableUsage objects

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

Example:

vars = [Simulink.VariableUsage('k','base workspace')
			Simulink.VariableUsage('myParam','base workspace')];
variablefilter = Simulink.findVars('MyModel',vars)

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: ‘FindUsedVars',false

'FindUsedVars' — Find variables that are used or not usedtrue (default) | false

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

Example: 'FindUsedVars',false

'IncludeEnumTypes' — Find enumerated types that are usedfalse (default) | true

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

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

Example: 'IncludeEnumTypes',true

'RegExp' — Enable regular expression matching'off' (default) | '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 string values.

Example: 'RegExp','on'

'SearchMethod' — Compile status'compiled' (default) | 'cached'

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'

'SearchReferencedModels' — Enable search in referenced models'off' (default) | 'on'

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

Example: 'SearchReferencedModels','on'

'Name' — Name of a variable or enumerated type to search forstring

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

Example: 'Name','trans'

Data Types: char

'SourceType' — Workspace or source defining the variables or enumerated typesstring

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'

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 true, '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.

'Users' — Name of block to search for variablesstring

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

To search a set of specific blocks, enable regular expression matching by specifying RegExp as 'on' and use regular expressions in the string. 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 the Stateflow® action language

  • S-functions that use data type variables registered using ssRegisterDataType

    To make the variables searchable, use ssRegisterTypeFromNamedObject instead.

  • Variables used by inactive variant subsystems

  • Variables referenced by machine-parented data in Stateflow

Was this topic helpful?