Documentation

Simulink.findVars

Analyze relationship between variables and blocks in models

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. For example, use this syntax to determine where a variable is used in a model.

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 unused variables. You can also search for enumerated data types that are used in context, in addition to variables.

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

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

Related Examples

Input Arguments

collapse all

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

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

collapse all

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

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

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.

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.

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'

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.

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

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 the Stateflow® action language

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

    To make the variables searchable, use ssRegisterTypeFromNamedObject instead.

  • Variables referenced by machine-parented data in Stateflow

Simulink.findVars discovers variable usage in inactive subsystem variants only if you select Analyze all choices during update diagram and generate preprocessor conditionals in the Variant Subsystem block dialog box. If you do not select this check box, the function does not discover variable usage in inactive variants.

Introduced in R2010a

Was this topic helpful?