Wednesday, September 28, 2022
HomeMatlabTime to transform from find_system to Simulink.findBlocks » Man on Simulink

Time to transform from find_system to Simulink.findBlocks » Man on Simulink


In case you are creating, modifying or analyzing Simulink fashions programmatically, you’re in all probability acquainted with find_system.

Whereas there’s nothing essentially mistaken with find_system, on this publish I wish to spotlight a set of features that present comparable capabilities with improved usability.

In MATLAB R2018a, we launched:

Let’s study why I consider these newer APIs supply a an improved usability.

Two Equal Queries

Let’s first have a look at a typical instance. Utilizing the instance sldemo_fuelsys, let’s examine the code to search out the one Math Perform block on this mannequin:

Utilizing find_system:

blksFindSystem = find_system(mdl,‘LookUnderMasks’,‘all’,‘FollowLinks’,‘on’,‘BlockType’,‘Math’)

Utilizing Simulink.findBlocksOfType:

opts = Simulink.FindOptions;

blksFindBlocks = Simulink.findBlocksOfType(mdl,‘Math’,opts);

getfullname(blksFindBlocks)

When this code, you’re in all probability questioning why I think about the second choice higher by way of usability. In spite of everything, it is 3 traces of code as a substitute of 1.

Let’s look into that in additional particulars.

find_system – Step by Step

Let’s undergo my thought course of after I write the road of code with find_system. I’ll first sort the return variable, the equal signal and begin typing “find_” till the suggestion mechanism gives find_system:

As soon as I settle for that, I sort the mannequin identify and see the following options:

With the function-style of find_system, the MATLAB Editor is ready to record the completely different syntaxes, however not the argument values. As first guess, I will in all probability go together with:

blksFindSystem = find_system(mdl,‘BlockType’,‘Math’)

Because it returns nothing, I have a look at the find_system documentation for extra particulars. The very first thing I decide is that the block I’m searching for is inside a library hyperlink, so I add the FollowLinks choice.

I copy the choice identify from the documentation and paste it in my line of code

blksFindSystem = find_system(mdl,‘FollowLinks’,‘on’,‘BlockType’,‘Math’)

Nonetheless nothing. Again to the documentation, I discover the LookUnderMasks choice

This time, I’m profitable:

blksFindSystem = find_system(mdl,‘FollowLinks’,‘on’,‘LookUnderMasks’,‘all’,‘BlockType’,‘Math’)

One factor to confess: even after writing code with find_system for 20 years, even when I do know precisely the choices I want, I at all times open the documentation and duplicate the choice names from there. The reason being in all probability a mixture of having made typos too usually and a choice to repeat and paste versus typing every little thing.

Simulink.findBlocksOfType – Step by Step

Let’s undergo the identical thought course of for Simulink.findBlocksOfType. As within the earlier case, I start typing the primary line

I settle for the suggestion.

opts = Simulink.FindOptions;

As soon as that is accomplished, I can hover the mouse over “opts” and instantly see the choices:

Shortly scanning them, I can see that LookUnderMasks is already set to “All“, however FollowLinks just isn’t enabled, so I begin typing:

I settle for the suggestion and end the road:

I begin typing the final line, accompanied by the code options all the best way:

blksFindBlocks = Simulink.findBlocksOfType(mdl,‘Math’,opts)

getfullname(blksFindBlocks)

Last Comparability

I hope I illustrated how, due to the newest enhancements to tab-completion and code options within the MATLAB Editor, Simulink.FindBlocks and associated features can enhance the coding expertise. It is 3 traces of code as a substitute of 1, however they’re simpler to jot down and as straightforward to know at first look.

Yet another benefit is after I have to name Simulink.findBlocks a number of occasions. Generally, I will create the Simulink.FindOptions object solely as soon as and reuse it many occasions in my code, simplifying issues much more.

Now it is your flip

It took me longer than it ought to have to understand how Simulink.findBlocks and associated features enhance my workflow in comparison with find_system. At first, I used to be kind of turned off by the necessity to name the extra operate Simulink.findOptions. Nevertheless, after forcing myself to make use of them just a few occasions, I shortly modified my thoughts and deserted find_system.

Give a attempt to Simulink.finBlocks and associated features and tell us what you suppose within the feedback beneath.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments