find_mdlrefs
Find referenced models and Model blocks in model hierarchy
Description
[
provides additional search options using one or more name-value pairs. For example, to keep the models loaded instead of temporarily loading them, setmodels
,blocks
] = find_mdlrefs(system
,Name,Value
)KeepModelsLoaded
totrue
.
Examples
Find Referenced Models in Model Hierarchy
Find referenced models and Model blocks for all models referenced by the specified model.
load_system('sldemo_mdlref_basic'); [myModels,myModelBlks] = find_mdlrefs('sldemo_mdlref_basic')
myModels =2x1 cell{'sldemo_mdlref_counter'} {'sldemo_mdlref_basic' }
myModelBlks =3 x1细胞{'sldemo_mdlref_basic/CounterA'} {'sldemo_mdlref_basic/CounterB'} {'sldemo_mdlref_basic/CounterC'}
Find and Load All Models in Model Hierarchy
By default, thefind_mdlrefs
function loads and then closes the models that were not already loaded. To identify what models are loaded, use thefind_system
function.
find_mdlrefs('sldemo_mdlref_depgraph'); find_system('type','block_diagram')
ans = 0x1 empty cell array
To find and load all models in the model hierarchy, setKeepModelsLoaded
totrue
.
find_mdlrefs('sldemo_mdlref_depgraph','KeepModelsLoaded',true); find_system('type','block_diagram')
ans =7x1 cell{'sldemo_mdlref_thermostat' } {'sldemo_mdlref_heater' } {'sldemo_mdlref_F2C' } {'sldemo_mdlref_outdoor_temp'} {'sldemo_mdlref_house' } {'sldemo_mdlref_heat2cost' } {'sldemo_mdlref_depgraph' }
The top model and all referenced models remain loaded. If you opensldemo_mdlref_depgraph
, you can navigate the model hierarchy without waiting for the referenced models to load as you open them.
Input Arguments
system
—System name, block path, or handle
character vector|string scalar|numeric scalar
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,Modelblock, orSubsystemblock.
If you specify a file name, do not include the file extension.
Data Types:double
|char
|string
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, whereName
is the argument name andValue
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 encloseName
in quotes.
Example:refModels = find_mdlrefs(topmodel,'KeepModelsLoaded',true,'ReturnTopModelAsLastElement',false)
KeepModelsLoaded
—Option to keep models loaded
false
or0
(default) |true
or1
Option to keep models loaded, specified as the comma-separated pair consisting of'KeepModelsLoaded'
and a numeric or logical1
(true
) or0
(false
).
By default the function loads and then closes the models that were not already loaded. To keep the models loaded, set this argument totrue
. Keeping the models loaded can be useful if you plan on interacting with the models after finding them.
Data Types:logical
AllLevels
—Levels to search
true
or1
(default) |false
or0
Levels to search, specified as the comma-separated pair consisting of'AllLevels'
and a numeric or logical1
(true
) or0
(false
).
true
— Search allModelblocks in the model hierarchy of the specifiedsystem
.false
— Search only the top-level system.
Data Types:logical
IncludeProtectedModels
—选项包括保护模型s
false
or0
(default) |true
or1
选项包括保护模型s in the search results, specified as the comma-separated pair consisting of'IncludeProtectedModels'
and a numeric or logical1
(true
) or0
(false
).
This setting only affects the returned list of referenced models; It does not affect the returned list ofModelblocks.
Data Types:logical
IncludeCommented
—Option to include commented blocks
false
or0
(default) |true
or1
Option to include commented blocks in the search results, specified as the comma-separated pair consisting of'IncludeCommented'
and a numeric or logical1
(true
) or0
(false
).
Data Types:logical
CaseSensitive
—Option to match case when searching
true
(default) |false
Option to match case when searching, specified astrue
for case-sensitive search orfalse
for case-insensitive search.
Data Types:logical
FollowLinks
—Option for search to follow library links
false
(default) |true
Option for the search to follow library links, specified astrue
orfalse
. Iftrue
, search follows links into library blocks.
Data Types:logical
LookUnderMasks
—Options to search masked blocks
'all'
(default) |'none'
|'functional'
|'graphical'
Options to search masked blocks, specified as:
'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.
Data Types:char
|string
MatchFilter
—Function handle that determines whether to include or skip elements in search
function handle
Function handle to match elements in a search, such as blocks, system, lines, ports, and annotations. UseMatchFilter
to determine whether elements should be included or skipped in the search.
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 only applies whenelement
is a subsystem. The default value isfalse
. If this value is set totrue
, the entire subsystem is omitted from the search.
For example, useMatchFilter
to find allModelblocks in a model for which theInitFcn
callback is defined using the filter function,initFcnMdlBlocks
:
functionmatch = initFcnMdlBlocks(handle) match = ~isempty(get_param(handle,'InitFcn'));end
addpath(fullfile(matlabroot,'examples','simulink_variants','main')); model='slexVariantMdlRefCondProp'; load_system(model); [models,blocks] = find_mdlrefs(model,'MatchFilter',@initFcnMdlBlocks)
Variants:To find active variants or code compile variant blocks, compile the model and apply the appropriate match filter function that Simulink®provides.
Simulink.match.activeVariants
— Match blocks that are active in simulation after model compilation.Simulink.match.codeCompileVariants
— Match blocks that are part of generated code after model compilation.
Note
To get correct results, you mustcompile the modelbefore usingSimulink.match.activeVariants
andSimulink.match.codeCompileVariants
filters. If the model is not compiled, these filters return all blocks in the model.
For example, use theSimulink.match.activeVariants
option to find active variants in a model.
addpath(fullfile(matlabroot,'examples','simulink_variants','main')); model='slexVariantMdlRefCondProp'; load_system(model); set_param(model,'SimulationCommand','update'); [models,blocks] = find_mdlrefs(model,'MatchFilter',@Simulink.match.activeVariants);
For example, use theSimulink.match.codeCompileVariants
option to find variant choices that are part of the generated C code.
addpath(fullfile(matlabroot,'examples','simulink_variants','main')); load_system('slexVariantMdlRefCondProp'); assignin('base','VSS_MODE',2); slexVariantMdlRefCondProp([],[],[],'compileForCodegen'); [models,blocks] = find_mdlrefs('slexVariantMdlRefCondProp',...'MatchFilter',@Simulink.match.codeCompileVariants); slexVariantMdlRefCondProp([],[],[],'term');
Variants
—Option to include variant models
'ActivePlusCodeVariants'
(default) |'ActiveVariants'
|'AllVariants'
Note
TheVariants
argument will be removed. UseMatchFilter
instead. For more information, seeCompatibility Considerations.
Option to include variant models in the search results, specified as the comma-separated pair consisting of'Variants'
and'ActivePlusCodeVariants'
,'ActiveVariants'
, or'AllVariants'
.
'ActivePlusCodeVariants'
— Include all variant models in theVariant Subsystemthat are active in simulation and is part of the generated code.'ActiveVariants'
— Include the active variant models in theVariant Subsystemblock.'AllVariants'
— Include all variant models in theVariant Subsystemblock.
This search constraint applies only toVariant Subsystemblocks that have theVariant control modeset toexpression
orlabel
. Use thefind_mdlrefs
function with theMatchFilter
option to operate on all types of variant blocks.
Data Types:char
|string
ReturnTopModelAsLastElement
—Option to include specified system
true
or1
(default) |false
or0
Option to include the specified system in the search results, specified as the comma-separated pair consisting of'ReturnTopModelAsLastElement'
and a numeric or logical1
(true
) or0
(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 specified with thesystem
argument. If you specify a block, the last element is the name of the file that contains it.
Data Types:logical
Output Arguments
models
— Names of models
cell array of character vectors
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 specified with thesystem
argument. If you specify a block, the last element is the model, library, or subsystem file that contains it.
blocks
— Names of Model blocks
cell array of character vectors
Names ofModel块,作为单元阵列的性格vect返回ors.
Version History
Introduced before R2006aR2021a:Variants
argument will be removed
Warns starting in R2021a
TheVariants
option will be removed fromfind_mdlrefs
in a future release. Scripts that use theVariants
option continue to work with a warning.
To findModelblocks that are active during simulation or code generation, compile the model and use thefind_mdlrefs
function with theMatchFilter
option.
R2021a: Default behavior has changed forfind_mdlrefs
withVariants
andMatchFilter
options in a model with variant blocks
Behavior changed in R2021a
Variants
: Using thefind_mdlrefs
functionwithouttheVariants
option includes only theModelblocks that are active during simulation or code generation in the search by default.考虑一个模型Variant Modelblock that has two variant choices,
Mdl_Linear_ Controller
andMdl_NonLinear_Controller
.This command returns only the activeModelblocks 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
: Using thefind_mdlrefs
function with theMatchFilter
option applies the filters on the active and inactive variant choices by default.考虑一个模型Variant Modelblock that has two variant choices,
Mdl_Linear_ Controller
andMdl_NonLinear_Controller
. The filter functioninitFcnMdlBlocks
finds all theModelblocks for which theInitFcn
callback is set.functionmatch = initFcnMdlBlocks(handle) match = ~isempty(get_param(handle,'InitFcn'));end
This command returns both the active and inactiveModelblocks 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'}
find_mdlrefs
does not support the use ofMatchFilter
along with theVariants
option.This command produces an error:
find_mdlrefs(bdroot,'MatchFilter',@Simulink.match.activeVariants,...'Variants','ActiveVariants');
R2020b: Specifying a logical value as the second argument offind_mdlrefs
is discouraged
Not recommended starting in R2020b
Thefind_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 theAllLevels
名称-值对。
Beispiel öffnen
Sie haben eine geänderte Version dieses Beispiels. Möchten Sie dieses Beispiel mit Ihren Änderungen öffnen?
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
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:.
你也可以希利ct 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)