Do You Find Yourself Searching for Connection (in Simulink)?
Greg’s pick this week is slQuery by Robert Rasche.
Robert has created an impressive mechanism for finding and operating on elements of Simulink models from MATLAB scripts and functions.
The part I feel he understates in his description of the entry is the ability to find blocks based on what the blocks are connected to, not just properties of the blocks themselves.
Once you get a handle on the query syntax, it’s pretty trivial to create complex queries of Simulink model blocks.
Find Blocks with Specific Properties
If you want to access blocks in a Simulink model, the standard APIs do enable you to locate blocks based on the properties of those blocks.
The standard API most users know about is the
If you want to search for a block based on property values, the standard API and
Find All Atomic Subsystems with Whitespace in the Name:
asbhl20_VehicleSystemsModel; blocks = slQuery('SubSystem[Name~=\w+\s+\w+, TreatAsAtomicUnit=on]'); % Replace whitespace in block names with underscore "_" names = regexprep(strtrim(blocks.Name), '\s+', '_')'; names(1:7)
ans = 7×1 cell array {'Air_Data_Computer' } {'Log_LL1_Actuator_Position_Sensors'} {'Log_LL2_Actuator_Position_Sensors'} {'Log_LU1_Actuator_Position_Sensors'} {'Log_LU2_Actuator_Position_Sensors'} {'Log_Hydraulic_System_1_Sensors' } {'Log_Hydraulic_System_2_Sensors' }
blocks = Simulink.findBlocksOfType(bdroot, 'SubSystem', ... 'Name', '\w+\s+\w+', ... 'TreatAsAtomicUnit', 'on', ... Simulink.FindOptions('RegExp', true)); % Replace whitespace in block names with underscore "_" names = regexprep(strtrim(get(blocks, 'Name')), '\s+', '_'); names(1:7)
ans = 7×1 cell array {'Air_Data_Computer' } {'Log_LL1_Actuator_Position_Sensors'} {'Log_LL2_Actuator_Position_Sensors'} {'Log_LU1_Actuator_Position_Sensors'} {'Log_LU2_Actuator_Position_Sensors'} {'Log_Hydraulic_System_1_Sensors' } {'Log_Hydraulic_System_2_Sensors' }
The standard API uses the addition of functional arguements to build a query, while
Find Blocks with Specific Connections
The slQuery function offers a way to search for blocks based on their connections to other elements in the Simulink model. This pretty much blows my mind!
In my 20 years of using Simulink, I could have definitely used features like this on several occasions.
This is something that the standard API functions
Find Inports directly connected to a Gain block
asbhl20; r = slQuery('Inport -> Gain'); cell2table([r(1).Parent.Name; r(1).Name; r(2).Name], ... 'RowNames', {'Parent', 'Inport', 'Gain'})
ans=3×4 table Var1 Var2 Var3 Var4 __________________________ ______ ______ _________________ Parent 'Low Altitude↵Intensity' 'Hqgw' 'Hrgw' 'cascvt' Inport 'Windspeed ↵at 20ft (6m)' 'V' 'V' 'In1' Gain 'sigma_wg ' 'pi/4' 'pi/3' 'Unit Conversion'
Note that this searches currently open Simulink model by default.
Find Feedback Loops
There are more sophisticated connection searches available, including the ability to look for feedback loops.
vdp;
r = slQuery('#vdp / Integrator >> $1') ;
r.Name
ans = 2×2 cell array {'vdp'} {'vdp'} {'x1' } {'x2' }
This syntax looks for a block that is connected to itself.
Why Did I Choose This Entry?
Firstly, I thought this was a really creative way to attack the problems Robert was facing. He leverages the extensibility of the MATLAB Language to great effect.
In addition, this File Exchange entry is documented, and provides a number of test cases; two elements that make it easier to understand how to use the
What Are Your Impressions of This Capability?
Let us know here.
- Category:
- Picks
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.