Some time ago, I published a post encouraging you to use Simulink.findBlocks instead of find_system. This week, I will take this a little bit further and show how to create a custom match filter to accomplish advanced searches. Introduction
If you are not familiar with creating match filters for Simulink.findBlock, I recommend first going through these documentation links to get the basic:
In short, a match filter is a function that takes as input block handles and returns a true or false value indicating if the block should be included in the search results or not. The Problem
I recently got a request for something slightly more complex than the examples in the above link, and I thought it would be interesting to share here. The question I received was more complex than that, but could be approximated to:
How to find all the blocks that are inside active variants and not of type Inport, Outport or Subsystem?
Here is what the model looks like:
The Solution
The technique I want to highlight here is to combine a built-in filter like Simulink.match.activeVariants with custom logic. More specifically:
- Use the prune output of the built-in filter Simulink.match.activeVariants to eliminate all the blocks inside inactive variants
- Filter out blocks of undesired types
This results in this match filter function:
We can test the match filter function:
opts = Simulink.FindOptions;
opts.MatchFilter = @myFilter;
VSS_MODE = 1;
set_param('sldemo_variant_subsystems','SimulationCommand','Update');
blks = Simulink.findBlocks('sldemo_variant_subsystems/Controller',opts);
getfullname(blks)
'sldemo_variant_subsystems/Controller/Linear Controller/Add'
'sldemo_variant_subsystems/Controller/Linear Controller/DiscreteTransfer …
Let's change the active variant and confirm that it returns different blocks:
VSS_MODE = 2;
set_param('sldemo_variant_subsystems','SimulationCommand','Update');
blks = Simulink.findBlocks('sldemo_variant_subsystems/Controller',opts);
getfullname(blks)
'sldemo_variant_subsystems/Controller/Nonlinear Controller/1-D Lookup Table'
'sldemo_variant_subsystems/Controller/Nonlinear Controller/Add'
Now it's your turn
Using a similar approach, it should be possible to create any complex logic.
Have you created custom match filters for Simulink.findBlocks? If yes, let us know what kind of search you are doing in the comments below.
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.