Searching in Simulink
Here we have an example library consisting of masked subsystems.
sys = 'pidLibrary';
open_system(sys)
When you double click on a block, you get the mask dialog, and the Help button displays the MaskHelp if you click on it.
Some of these blocks are properly documented, but others have no MaskHelp. You could click through all the blocks in this library and click on the help button, but that would be a painful manual process. How can you audit this library and ensure that the components are properly documented?
find_system
find_system is a very powerful tool for scripting in Simulink which allows you to automate tasks that might otherwise be time-consuming to perform. The find_system syntax is very specific:
find_system(sys, 'c1', cv1, 'c2', cv2,...'p1', v1, 'p2', v2,...)
find_system takes a set of constraints ('c1', cv1, 'c2', cv2) and searches for blocks in the system (sys) that have the specified parameter values ('p1', v1, 'p2', v2). Notice that all constraint/value pairs are passed to find_system before the parameter/value pairs. This confused me once when I got the order wrong, so watch out for that.
Here is a quick find_system command that returns the right answer.
blksNoHelp = find_system(sys,'MaskHelp','') hilite_system(blksNoHelp,'find')
blksNoHelp = 'pidLibrary/Continuous PI Controller' 'pidLibrary/Discrete PI Controller'
It is important to be aware of the search constraint defaults.
- SearchDepth is set to all levels, but this can be changed to 0 for only open systems, 1 to search only the specified system, 2 for that system and its children, etc.
- LookUnderMasks defaults to graphical only, which means find_system doesn’t look under functional masks (those with a dialog and workspace).
- FollowLinks defaults to off, which means linked blocks are not searched. This is very important when the block might reside in a library linked from your system.
- FindAll defaults to off, which means you will only get blocks back from the search. The other option is to specify on and find_system will also return lines and annotations from the model.
- CaseSensitive is on by default, so make sure your caps lock isn’t turned on. This constraint affects the value inputs only; parameters are not treated as being case sensitive.
In our example, if we were also concerned with the contents of those masked blocks, we would need to expand our search to LookUnderMasks. This search would then return Simulink base blocks because they do not use MaskHelp. To reduce the results to only those blocks that are masked, we can do a search where the Mask property is set to on.
blksNoHelp2 = find_system(sys,'LookUnderMasks','all',... 'Mask','on','MaskHelp','') hilite_system(setdiff(blksNoHelp2,blksNoHelp),'find')
blksNoHelp2 = 'pidLibrary/Continuous PI Controller' 'pidLibrary/Discrete PI Controller' 'pidLibrary/Discrete PID Controller/Discrete Derivative'
Another reason constraints are so important is that the amount of work done by find_system is proportional to the number of blocks included in the search. If you are looking under all masks, following all links and searching the full depth of the model hierarchy, the speed of the search will be proportional to the size of your model. find_system is not a Simulink Google search that skips across indexed model information. When find_system is called it has to resolve all block parameters as it works. This means that you will evaluate all the dialog code and parameters required by the search. To speed up find_system, include the appropriate constraints to reduce the number of blocks being evaluated.
Tip: Where ever possible include a SearchDepth to cast a small net.
Challenge
This got me thinking about how you might perform the inverse of this search. How would you find blocks that have MaskHelp without specifying the value it is set to? The first person to provide a correct answer in the comments gains fame and notoriety as a find_system expert.
Did you already know how to use find_system? This is my second post and I’m looking for feedback on how much experience you have with Simulink. Post a comment and tell me what you think.
- Category:
- Commands
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.