find_mdlrefs
Find referenced models and Model blocks in model hierarchy
Description
[
specifies options using one or more name-value arguments. For example, to keep the
models loaded instead of temporarily loading them, set
mdls
,blks
]
= find_mdlrefs(sys
,Name=Value
)KeepModelsLoaded
to true
.
Examples
Find referenced models and Model blocks for all models referenced by the specified model.
mdl = "sldemo_mdlref_basic";
load_system(mdl);
[myModels,myModelBlks] = find_mdlrefs(mdl)
myModels = 2×1 cell
{'sldemo_mdlref_counter'}
{'sldemo_mdlref_basic' }
myModelBlks = 3×1 cell
{'sldemo_mdlref_basic/CounterA'}
{'sldemo_mdlref_basic/CounterB'}
{'sldemo_mdlref_basic/CounterC'}
By default, the find_mdlrefs
function loads and then closes the models that were not already loaded.
Open the project that contains the sldemo_mdlref_depgraph
model.
openProject("ModelReferenceHierarchy");
This project opens the sldemo_mdlref_depgraph
model.
To identify the loaded models, use the find_system
function.
find_system(type="block_diagram")
ans = 2×1 cell
{'simulink_extras' }
{'sldemo_mdlref_depgraph'}
To find all the models in the model hierarchy, use the find_mdlrefs
function.
mdl = "sldemo_mdlref_depgraph";
find_mdlrefs(mdl)
ans = 7×1 cell
{'sldemo_mdlref_heat2cost' }
{'sldemo_mdlref_house' }
{'sldemo_mdlref_F2C' }
{'sldemo_mdlref_outdoor_temp'}
{'sldemo_mdlref_thermostat' }
{'sldemo_mdlref_heater' }
{'sldemo_mdlref_depgraph' }
To identify the models that remain loaded, use the find_system
function.
find_system(type="block_diagram")
ans = 2×1 cell
{'simulink_extras' }
{'sldemo_mdlref_depgraph'}
Only the previously loaded models remain loaded.
To find and load all models in the model hierarchy, use the find_mdlrefs
function. Set KeepModelsLoaded
to true
.
find_mdlrefs(mdl, KeepModelsLoaded=true);
find_system(type="block_diagram")
ans = 8×1 cell
{'sldemo_mdlref_thermostat' }
{'sldemo_mdlref_heater' }
{'sldemo_mdlref_F2C' }
{'sldemo_mdlref_outdoor_temp'}
{'sldemo_mdlref_house' }
{'sldemo_mdlref_heat2cost' }
{'simulink_extras' }
{'sldemo_mdlref_depgraph' }
The top model and all referenced models remain loaded. With all the referenced models loaded, you can navigate the model hierarchy without waiting for each referenced model to load as you open it.
Use the MatchFilter argument with a custom filter function to find all Model blocks for which the InitFcn
callback is defined.
The custom function is defined in the file initFcnMdlBlocks.m
.
type initFcnMdlBlocks.m
function match = initFcnMdlBlocks(handle) match = ~isempty(get_param(handle,'InitFcn')); end
Provide the function handle as the value of the MatchFilter
argument.
mdl = "slexVariantMdlRefCondProp";
load_system(mdl);
[mdls,blks] = find_mdlrefs(mdl, MatchFilter=@initFcnMdlBlocks)
mdls = 1×1 cell array
{'slexVariantMdlRefCondProp'}
blks = 0×0 empty cell array
To find variant blocks that are active in simulation after model compilation, use the built-in filter function named Simulink.match.activeVariants
.
model1 = "slexVariantMdlRefCondProp"; load_system(model1); set_param(model1, SimulationCommand="update"); [models,blocks] = find_mdlrefs(model1, ... MatchFilter=@Simulink.match.activeVariants)
models = 3×1 cell
{'slexVariantMdlRefCondProp_sub1'}
{'slexVariantMdlRefCondProp_sub2'}
{'slexVariantMdlRefCondProp' }
blocks = 2×1 cell
{'slexVariantMdlRefCondProp/sub1'}
{'slexVariantMdlRefCondProp/sub2'}
To find the variant choices that are part of the generated C code after model compilation, use the built-in filter function named Simulink.match.codeCompileVariants
.
assignin("base", VSS_MODE=2); slexVariantMdlRefCondProp([],[],[],"compileForCodegen");
### Searching for referenced models in model 'slexVariantMdlRefCondProp'. ### Total of 3 models to build. ### Starting serial code generation build. ### Starting build procedure for: slexVariantMdlRefCondProp_sub1 ### Successful completion of build procedure for: slexVariantMdlRefCondProp_sub1 ### Starting build procedure for: slexVariantMdlRefCondProp_sub2 ### Successful completion of build procedure for: slexVariantMdlRefCondProp_sub2 Build Summary Model reference code generation targets: Model Build Reason Status Build Duration ====================================================================================================================================== slexVariantMdlRefCondProp_sub1 Target (slexVariantMdlRefCondProp_sub1.c) did not exist. Code generated and compiled. 0h 0m 14.911s slexVariantMdlRefCondProp_sub2 Target (slexVariantMdlRefCondProp_sub2.c) did not exist. Code generated and compiled. 0h 0m 5.8696s 2 of 2 models built (0 models already up to date) Build duration: 0h 0m 22.001s
[models,blocks] = find_mdlrefs(model1, ...
MatchFilter=@Simulink.match.codeCompileVariants)
models = 3×1 cell
{'slexVariantMdlRefCondProp_sub1'}
{'slexVariantMdlRefCondProp_sub2'}
{'slexVariantMdlRefCondProp' }
blocks = 2×1 cell
{'slexVariantMdlRefCondProp/sub1'}
{'slexVariantMdlRefCondProp/sub2'}
slexVariantMdlRefCondProp([],[],[],"term");
To find all blocks irrespective of whether a block is active or inactive due to variants, use the built-in filter function named Simulink.match.allVariants
.
[models,blocks] = find_mdlrefs(model1, ...
MatchFilter=@Simulink.match.allVariants)
models = 3×1 cell
{'slexVariantMdlRefCondProp_sub1'}
{'slexVariantMdlRefCondProp_sub2'}
{'slexVariantMdlRefCondProp' }
blocks = 2×1 cell
{'slexVariantMdlRefCondProp/sub1'}
{'slexVariantMdlRefCondProp/sub2'}
To find Variant Subsystem block choices that are active in simulation or part of generated code at edit time, use the built-in filter function named Simulink.match.legacy.filterOutCodeInactiveVariantSubsystemChoices
.
model2 = 'sldemo_mdlref_variants'; load_system(model2); [models,blocks] = find_mdlrefs(model2, ... MatchFilter=@Simulink.match.legacy.filterOutCodeInactiveVariantSubsystemChoices)
models = 3×1 cell
{'sldemo_mrv_linear_controller' }
{'sldemo_mrv_nonlinear_controller'}
{'sldemo_mdlref_variants' }
blocks = 2×1 cell
{'sldemo_mdlref_variants/Controller/Linear' }
{'sldemo_mdlref_variants/Controller/Nonlinear'}
To find active Variant Subsystem block choices at edit time, use the built-in filter function named Simulink.match.legacy.filterOutInactiveVariantSubsystemChoices
.
[models,blocks] = find_mdlrefs(model2, ...
MatchFilter=@Simulink.match.legacy.filterOutInactiveVariantSubsystemChoices)
models = 2×1 cell
{'sldemo_mrv_nonlinear_controller'}
{'sldemo_mdlref_variants' }
blocks = 1×1 cell array
{'sldemo_mdlref_variants/Controller/Nonlinear'}
For information on the limitations of edit-time filters, see MatchFilter.
Input Arguments
System name, block path, or handle, specified as a character vector, string scalar, or numeric scalar.
The system must be an SLX file, MDL file, Model block, or Subsystem block.
Before R2025a: If you specify a filename, do not include the file extension.
Data Types: double
| char
| string
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.
Example: refModels =
find_mdlrefs(topmodel,KeepModelsLoaded=true,ReturnTopModelAsLastElement=false)
Option to keep models loaded, specified as a numeric or logical 1
(true
) or 0
(false
).
By default, the function loads and then closes the models that were not already loaded.
To keep the models loaded, set this argument to true
.
Keeping the models loaded can be useful if you plan on interacting with
the models after finding them.
Data Types: logical
Levels to search, specified as a numeric or logical 1
(true
) or 0
(false
).
true
— Search all Model blocks in the model hierarchy of the specified system.false
— Search only the top-level system.
Data Types: logical
Option to include protected models in the search results, specified as a numeric or
logical 1
(true
) or
0
(false
).
This setting affects only the returned list of referenced models. This setting does not affect the returned list of Model blocks.
Data Types: logical
Option to include commented blocks in the search results, specified as a numeric or
logical 1
(true
) or
0
(false
).
Data Types: logical
Option for the search to follow library links, specified as true
or
false
. If true
, the search
follows links into library blocks.
Data Types: logical
Options to search masked blocks, specified as "all"
,
"none"
, "functional"
, or
"graphical"
.
"all"
— Search in all masked blocks."none"
— Prevent searching in masked systems."functional"
— Include masked subsystems that do not have dialogs."graphical"
— Include masked subsystems that do not have workspaces or dialogs.
Option to match and filter elements such as blocks, systems, lines,
port, and annotations in search, specified as a function handle. Use
MatchFilter
to determine whether elements
should be included or skipped in the search.
The argument:
Allows you to filter elements with custom filter functions
Avoids processing elements when filters do not match
Applies complex filters on blocks, lines, or annotations, to filter the results internally
The named function must be defined within a MATLAB® program file. The function takes the handle of the element as input and returns two outputs.
function [match, prune] = func(element)
The input
element
is the handle of the block being processed.The first output,
match
, is a logical value. Iffalse
, search skips the element.The second output,
prune
, is an optional logical value that applies only whenelement
is a subsystem. The default value isfalse
. If this value is set totrue
, the entire subsystem is omitted from the search.
To find variant blocks, the software provides these post-compile filter functions:
Simulink.match.activeVariants
— Filter function to find blocks that are active in simulation after model compilationSimulink.match.codeCompileVariants
— Filter function to find blocks that are part of generated code after model compilationSimulink.match.allVariants
— Filter function to find all blocks irrespective of whether a block is active or inactive due to variants
Note
To get correct results, you must compile the model before using
the Simulink.match.activeVariants
and
Simulink.match.codeCompileVariants
filters.
If the model is not compiled, these filters return all blocks in the
model. For an example that compares the pre-compile and post-compile
results for these filters, see Compare Pre-Compile and Post-Compile Behavior of Match Filters for Variant Blocks.
For Variant Subsystem blocks, the software provides these edit-time filter functions:
Simulink.match.legacy.filterOutCodeInactiveVariantSubsystemChoices
— Filter function to find Variant Subsystem block choices that are active in simulation or part of generated codeSimulink.match.legacy.filterOutInactiveVariantSubsystemChoices
— Filter function to find active Variant Subsystem block choices
Edit-time filters:
Do not use the post-compile block activeness information in the CompiledVariantInfo block parameter
Apply to only Variant Subsystem blocks that have Variant control mode set to
expression
orlabel
and Propagate conditions outside of variant subsystem set tooff
Can identify whether a block handle is inside the active choice of a Variant Subsystem block only when used within the context of the
find_system
,find_mdlrefs
, andSimulink.FindOptions
functions
To operate on all types of variant blocks, use the
Simulink.match.codeCompileVariants
or
Simulink.match.activeVariants
filters after model
compilation.
Data Types: function_handle
Since R2025a
Diagnostic action to take for invalid model names, specified as
"error"
, "warning"
, or
"ignore"
.
"error"
— When the software finds an invalid model name or missing referenced model, you receive an error."warning"
— When the software finds an invalid model name or missing referenced model, you receive a warning."ignore"
— The software ignores invalid model names and missing referenced models.
Option to include the specified system in the search results, specified as a numeric or
logical 1
(true
) or
0
(false
).
By default, the last element in the returned list of referenced models is the name of
the model, library, or subsystem file that you specify with the
sys
argument. If you specify a block, the last
element is the name of the file that contains it.
Data Types: logical
Output Arguments
Names of models, returned as a cell array of character vectors.
By default, the last element is the name of the model, library, or subsystem file that
you specify with the sys
argument. If you specify a
block, the last element is the model, library, or subsystem file that
contains it.
Names of Model blocks, returned as a cell array of character vectors.
Version History
Introduced before R2006aTo control whether the find_mdlrefs
function provides an
error, warning, or no diagnostic for invalid model names, set
InvalidModelNames
to "error"
,
"warning"
, or "ignore"
.
When you specify a filename for sys
, you can now specify the
filename with or without the extension. Previously, you could not specify a filename
with an extension.
When the software encounters an error while executing the
find_mdlrefs
function, the
KeepModelsLoaded
value now determines whether the software
closes the models that the function loads. By default, the software closes the
models that the function loads.
The find_mdlrefs
function now consistently provides the
output model names as a 1-by-n cell array. Previously, when the
function returned no model names, the output was a 0-by-1 cell array instead of a
1-by-0 cell array.
The Variants
argument will be removed in a future release. Use the
MatchFilter
argument instead.
The MatchFilter
argument lets you:
Find Model blocks that are active during simulation or code generation after compiling the model.
Meanwhile, the
Variants
argument produces inconsistent search results. Thefind_mdlrefs
function is an edit-time operation, but you must compile the model to determine whether a block is active in a model with all types of variant blocks.Operate on all types of variant blocks.
Meanwhile, the
Variants
argument applies to only Variant Subsystem blocks that have the Variant control mode set toexpression
orlabel
.
In R2021a, this removal was announced.
Starting in R2022b, you receive a warning when:
You use the
find_mdlrefs
function without theVariants
argument, and the function skips the inactive choice of a Variant Subsystem block during the search.You use the
find_mdlrefs
function withVariants
set to'AllVariants'
.
This table lists the recommended replacements for different values of the
Variants
argument.
To Be Removed | Recommended Replacement |
---|---|
[mdls,blks] = find_mdlrefs(mdl, ... 'Variants','ActiveVariants') |
set_param(mdl, SimulationCommand="update"); [mdls,blks] = find_mdlrefs(mdl, ... MatchFilter=@Simulink.match.activeVariants) |
[mdls,blks] = find_mdlrefs(mdl, ... 'Variants','ActivePlusCodeVariants') |
mdlname([],[],[],'compileForCodegen'); [mdls,blks] = find_mdlrefs(mdl, ... MatchFilter=@Simulink.match.codeCompileVariants); mdlname([],[],[],'term') |
[mdls,blks] = find_mdlrefs(mdl, ... 'Variants','AllVariants') |
[mdls,blks] = find_mdlrefs(mdl, ...
MatchFilter=@Simulink.match.allVariants) |
When you use the find_mdlrefs
function, you cannot specify
both the MatchFilter
and Variants
arguments.
You can use the built-in match filter
Simulink.match.allVariants
to find all the blocks in a
variant model regardless of whether the block is active or inactive due to variants.
This filter is the recommended replacement for the 'AllVariants'
option.
To Be Removed | Recommended Replacement |
---|---|
[mdls,blks] = find_mdlrefs(mdl, ... 'Variants','AllVariants') |
[mdls,blks] = find_mdlrefs(mdl, ...
MatchFilter=@Simulink.match.allVariants) |
Variants
: When you use thefind_mdlrefs
function without theVariants
argument, for Variant Subsystem blocks, the function currently includes only those choices that are active during simulation or code generation in the search by default.For other variant blocks such as Variant Source, Variant Sink, or Variant Subsystem blocks with the Propagate conditions outside of variant subsystem parameter set to
on
, the function includes all choices in the search.Consider a model with a Variant Model block that has two variant choices,
Mdl_Linear_Controller
andMdl_NonLinear_Controller
.This command returns only the active Model blocks in the model.
[myModels,myModelBlks] = find_mdlrefs... ('sldemo_variant_subsystems_modelblocks')
myModels = 2×1 cell array {'mdlref_nonlinear_controller' } {'sldemo_variant_subsystems_modelblocks'} myModelBlks = 1×1 cell array {'sldemo_variant_subsystems_modelblocks/Controller/Mdl_NonLinear_Controller'}
MatchFilter
: When you use thefind_mdlrefs
function with theMatchFilter
argument, the function applies the filters on the active and inactive variant choices by default.Consider a model with a Variant Model block that has two variant choices,
Mdl_Linear_ Controller
andMdl_NonLinear_Controller
. The filter functioninitFcnMdlBlocks
finds all the Model blocks for which theInitFcn
callback is set.function match = initFcnMdlBlocks(handle) match = ~isempty(get_param(handle, 'InitFcn')); end
This command returns the active and inactive Model blocks in the model.
[myModels,myModelBlks] = find_mdlrefs('sldemo_variant_subsystems_modelblocks',... 'MatchFilter', @initFcnMdlBlocks)
myModels = 3×1 cell array {'mdlref_linear_controller' } {'mdlref_nonlinear_controller' } {'sldemo_variant_subsystems_modelblocks'} myModelBlks = 2×1 cell array {'sldemo_variant_subsystems_modelblocks/Controller/Mdl_Linear_Controller' } {'sldemo_variant_subsystems_modelblocks/Controller/Mdl_NonLinear_Controller'}
To match and filter model elements during a search, you can define a custom filter function
and pass the function handle as value to the MatchFilter
argument.
To find variant blocks that are active in a simulation or part of the generated code, after
compiling the model, use the built-in match filter functions named
Simulink.match.activeVariants
,
Simulink.match.codeCompileVariants
, and
Simulink.match.allVariants
.
The find_mdlrefs
function provides two ways to specify
whether to search all levels of the model hierarchy. Both techniques give the same
results, but only the name-value pair technique allows you to specify additional
options.
Instead of specifying whether to search all levels of the model hierarchy with a
logical as the second argument, use the AllLevels
name-value
pair.
See Also
Blocks
Functions
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)