Main Content

find_mdlrefs

Find referenced models and Model blocks in model hierarchy

Description

example

[models,blocks] = find_mdlrefs(system) finds all referenced models and Model blocks in the model hierarchy below the specified system. The find_mdlrefs function temporarily loads the models.

example

[models,blocks] = find_mdlrefs(system,Name,Value) provides additional search options using one or more name-value pairs. For example, to keep the models loaded instead of temporarily loading them, set KeepModelsLoaded to true.

Examples

collapse all

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 = 3x1 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 = 2x1 cell
    {'simulink_extras'       }
    {'sldemo_mdlref_depgraph'}

To find all the models in the model hierarchy, use the find_mdlrefs function.

find_mdlrefs('sldemo_mdlref_depgraph')
ans = 7x1 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 = 2x1 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, and set KeepModelsLoaded to true.

find_mdlrefs('sldemo_mdlref_depgraph','KeepModelsLoaded',true);
find_system('type','block_diagram')
ans = 8x1 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.

function match = initFcnMdlBlocks(handle)
  match = ~isempty(get_param(handle,'InitFcn'));
end

Provide the function handle as the value of the MatchFilter argument.

load_system('slexVariantMdlRefCondProp');
[models,blocks] = find_mdlrefs('slexVariantMdlRefCondProp','MatchFilter',@initFcnMdlBlocks);

Simulink.match.activeVariants

Find variant blocks that are active in simulation after model compilation.

model1='slexVariantMdlRefCondProp';
load_system(model1);
set_param(model1,'SimulationCommand','update');
[models,blocks] = find_mdlrefs(model1,'MatchFilter',@Simulink.match.activeVariants);

Simulink.match.codeCompileVariants

Find variant choices that are part of the generated C code after model compilation.

assignin('base','VSS_MODE',2);
slexVariantMdlRefCondProp([],[],[],'compileForCodegen');
### Searching for referenced models in model 'slexVariantMdlRefCondProp'.
### Found 2 model references to update.
### Starting serial model reference code generation build.
### Checking status of model reference code generation target for model 'slexVariantMdlRefCondProp_sub1' used in 'slexVariantMdlRefCondProp'.
### Model reference code generation target (slexVariantMdlRefCondProp_sub1.c) for model slexVariantMdlRefCondProp_sub1 is out of date because slexVariantMdlRefCondProp_sub1.c does not exist.
### Starting build procedure for: slexVariantMdlRefCondProp_sub1
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: /tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slprj/ert/slexVariantMdlRefCondProp_sub1
### Invoking Target Language Compiler on slexVariantMdlRefCondProp_sub1.rtw
### Using System Target File: /mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/ert/ert.tlc
### Loading TLC function libraries
.......
### Initial pass through model to cache user defined code
### Caching model source code
................................
### Writing header file slexVariantMdlRefCondProp_sub1_types.h
### Writing header file slexVariantMdlRefCondProp_sub1.h
### Writing header file rtwtypes.h
.
### Writing source file slexVariantMdlRefCondProp_sub1.c
### Writing header file slexVariantMdlRefCondProp_sub1_private.h
### TLC code generation complete (took 6.1s).
### Creating HTML report file index.html
### Saving binary information cache.
### Using toolchain: GNU gcc/g++ | gmake (64-bit Linux)
### Creating '/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slprj/ert/slexVariantMdlRefCondProp_sub1/slexVariantMdlRefCondProp_sub1.mk' ...
### Building 'slexVariantMdlRefCondProp_sub1_rtwlib': "/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/bin/glnxa64/gmake"  -f slexVariantMdlRefCondProp_sub1.mk all
gcc -c -fwrapv -fPIC -O0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0  -DTID01EQ=0 -DMODEL=slexVariantMdlRefCondProp_sub1 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 -I/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893 -I/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slprj/ert/slexVariantMdlRefCondProp_sub1 -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/extern/include -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/simulink/include -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/src -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/src/ext_mode/common -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/ert -I/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slprj/ert/_sharedutils -o "slexVariantMdlRefCondProp_sub1.o" "/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slprj/ert/slexVariantMdlRefCondProp_sub1/slexVariantMdlRefCondProp_sub1.c"
### Creating static library ./slexVariantMdlRefCondProp_sub1_rtwlib.a ...
ar ruvs  ./slexVariantMdlRefCondProp_sub1_rtwlib.a slexVariantMdlRefCondProp_sub1.o
ar: creating ./slexVariantMdlRefCondProp_sub1_rtwlib.a
a - slexVariantMdlRefCondProp_sub1.o
### Created: ./slexVariantMdlRefCondProp_sub1_rtwlib.a
### Successfully generated all binary outputs.
gmake: Nothing to be done for `all'.
### Successful completion of build procedure for: slexVariantMdlRefCondProp_sub1
### Checking status of model reference code generation target for model 'slexVariantMdlRefCondProp_sub2' used in 'slexVariantMdlRefCondProp'.
### Model reference code generation target (slexVariantMdlRefCondProp_sub2.c) for model slexVariantMdlRefCondProp_sub2 is out of date because slexVariantMdlRefCondProp_sub2.c does not exist.
### Starting build procedure for: slexVariantMdlRefCondProp_sub2
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: /tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slprj/ert/slexVariantMdlRefCondProp_sub2
### Invoking Target Language Compiler on slexVariantMdlRefCondProp_sub2.rtw
### Using System Target File: /mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/ert/ert.tlc
### Loading TLC function libraries
.......
### Generating TLC interface API for custom data
.
### Initial pass through model to cache user defined code
.
### Caching model source code
........................................
### Writing header file slexVariantMdlRefCondProp_sub2_types.h
### Writing header file slexVariantMdlRefCondProp_sub2.h
.
### Writing header file variant_defines.h
### Writing source file slexVariantMdlRefCondProp_sub2.c
### Writing header file slexVariantMdlRefCondProp_sub2_private.h
### TLC code generation complete (took 6.127s).
### Creating HTML report file index.html
### Saving binary information cache.
### Using toolchain: GNU gcc/g++ | gmake (64-bit Linux)
### Creating '/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slprj/ert/slexVariantMdlRefCondProp_sub2/slexVariantMdlRefCondProp_sub2.mk' ...
### Building 'slexVariantMdlRefCondProp_sub2_rtwlib': "/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/bin/glnxa64/gmake"  -f slexVariantMdlRefCondProp_sub2.mk all
gcc -c -fwrapv -fPIC -O0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0  -DTID01EQ=0 -DMODEL=slexVariantMdlRefCondProp_sub2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 -I/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893 -I/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slprj/ert/slexVariantMdlRefCondProp_sub2 -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/extern/include -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/simulink/include -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/src -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/src/ext_mode/common -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/ert -I/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slprj/ert/_sharedutils -o "slexVariantMdlRefCondProp_sub2.o" "/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slprj/ert/slexVariantMdlRefCondProp_sub2/slexVariantMdlRefCondProp_sub2.c"
### Creating static library ./slexVariantMdlRefCondProp_sub2_rtwlib.a ...
ar ruvs  ./slexVariantMdlRefCondProp_sub2_rtwlib.a slexVariantMdlRefCondProp_sub2.o
ar: creating ./slexVariantMdlRefCondProp_sub2_rtwlib.a
a - slexVariantMdlRefCondProp_sub2.o
### Created: ./slexVariantMdlRefCondProp_sub2_rtwlib.a
### Successfully generated all binary outputs.
gmake: Nothing to be done for `all'.
### Successful completion of build procedure for: slexVariantMdlRefCondProp_sub2
### Simulink cache artifacts for 'slexVariantMdlRefCondProp_sub1' were created in '/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slexVariantMdlRefCondProp_sub1.slxc'.
### Simulink cache artifacts for 'slexVariantMdlRefCondProp_sub2' were created in '/tmp/Bdoc24a_2528353_1572643/tp5a19a65a/simulink_variants-ex24779893/slexVariantMdlRefCondProp_sub2.slxc'.

Build Summary

Code generation targets built:

Model                           Action                        Rebuild Reason                                    
================================================================================================================
slexVariantMdlRefCondProp_sub1  Code generated and compiled.  slexVariantMdlRefCondProp_sub1.c does not exist.  
slexVariantMdlRefCondProp_sub2  Code generated and compiled.  slexVariantMdlRefCondProp_sub2.c does not exist.  

2 of 2 models built (0 models already up to date)
Build duration: 0h 0m 49.294s
[models,blocks] = find_mdlrefs(model1,...
                               'MatchFilter',@Simulink.match.codeCompileVariants);
slexVariantMdlRefCondProp([],[],[],'term');

Simulink.match.allVariants

Find all blocks irrespective of whether the block is active or inactive due to variants.

[models,blocks] = find_mdlrefs(model1,'MatchFilter',@Simulink.match.allVariants);

Simulink.match.legacy.filterOutCodeInactiveVariantSubsystemChoices

Find Variant Subsystem choice blocks that are active in simulation or part of generated code at edit-time. For information on the limitations of edit-time filters, see MatchFilter.

model2='sldemo_mdlref_variants';
load_system(model2);
[models,blocks] = find_mdlrefs(model2, ...
   'MatchFilter',@Simulink.match.legacy.filterOutCodeInactiveVariantSubsystemChoices)

Simulink.match.legacy.filterOutInactiveVariantSubsystemChoices

Find active Variant Subsystem choice blocks at edit-time. For information on the limitations of edit-time filters, see MatchFilter.

[models,blocks] = find_mdlrefs(model2, ...
   'MatchFilter',@Simulink.match.legacy.filterOutInactiveVariantSubsystemChoices)

Input Arguments

collapse all

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.

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 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: refModels = find_mdlrefs(topmodel,'KeepModelsLoaded',true,'ReturnTopModelAsLastElement',false)

Option to keep models loaded, specified as the comma-separated pair consisting of 'KeepModelsLoaded' and 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 the comma-separated pair consisting of 'AllLevels' and 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 the comma-separated pair consisting of 'IncludeProtectedModels' and a numeric or logical 1 (true) or 0 (false).

This setting only affects the returned list of referenced models; It does not affect the returned list of Model blocks.

Data Types: logical

Option to include commented blocks in the search results, specified as the comma-separated pair consisting of 'IncludeCommented' and a numeric or logical 1 (true) or 0 (false).

Data Types: logical

Option to match case when searching, specified as true for case-sensitive search or false for case-insensitive search.

Data Types: logical

Option for the search to follow library links, specified as true or false. If true, search follows links into library blocks.

Data Types: logical

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

Option to match and filter elements such as blocks, system, lines, ports, and annotations in a search, specified as 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. If false, search skips the element.

  • The second output, prune, is an optional logical value that only applies when element is a subsystem. The default value is false. If this value is set to true, the entire subsystem is omitted from the search.

Variants: Simulink® provides these built-in match filter functions to find variant blocks.

Post-compile time filter functions:

  • Simulink.match.activeVariants — Filter function to find blocks that are active in simulation after model compilation.

  • Simulink.match.codeCompileVariants — Filter function to find blocks that are part of generated code after model compilation.

  • Simulink.match.allVariants — Filter function to find all blocks irrespective of whether the block is active or inactive due to variants.

Note

To get correct results, you must compile the model before using Simulink.match.activeVariants and Simulink.match.codeCompileVariants filters. If the model is not compiled, these filters return all blocks in the model.

Edit-time filter functions for Variant Subsystem blocks:

  • Simulink.match.legacy.filterOutCodeInactiveVariantSubsystemChoices — Filter function to find Variant Subsystem choice blocks that are active in simulation or part of generated code. This function produces similar results as the 'ActivePlusCodeVariants' option of the Variants argument.

  • Simulink.match.legacy.filterOutInactiveVariantSubsystemChoices — Filter function to find active Variant Subsystem choice blocks. This function produces similar results as the 'ActiveVariants' option of the Variants argument.

Limitations of edit-time filters:

  • The filters do not use the post-compile block activeness information in the CompiledVariantInfo block parameter.

  • The filters apply only to Variant Subsystem blocks that have these block parameter settings:

    • Variant control mode set to expression or label

    • Propagate conditions outside of variant subsystem set to off

  • The filters can identify if a block handle is inside the active choice of a Variant Subsystem only when used within the context of find_system, find_mdlrefs, and Simulink.FindOptions.

To operate on all types of variant blocks, use the Simulink.match.codeCompileVariants or Simulink.match.activeVariants filters after model compilation.

Note

The Variants argument will be removed. Use MatchFilter instead. For more information, see Compatibility 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 the Variant Subsystem that are active in simulation and is part of the generated code.

  • 'ActiveVariants' — Include the active variant models in the Variant Subsystem block.

  • 'AllVariants' — Include all variant models in the Variant Subsystem block.

This search constraint applies only to Variant Subsystem blocks that have the Variant control mode set to expression or label. Use the find_mdlrefs function with the MatchFilter option to operate on all types of variant blocks.

Data Types: char | string

Option to include the specified system in the search results, specified as the comma-separated pair consisting of 'ReturnTopModelAsLastElement' and 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 specified with the system argument. If you specify a block, the last element is the name of the file that contains it.

Data Types: logical

Output Arguments

collapse all

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 the system 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 R2006a

expand all