File Exchange Pick of the Week

Our best user submissions

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Do You Find Yourself Searching for Connection (in Simulink)?

Posted by Guest Picker,

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 find_system function. A more recent addition is the Simulink.findBlocks function which provides the same functionality as find_system.

If you want to search for a block based on property values, the standard API and slQuery offer the same capabilities, though through two different approaches.

Find All Atomic Subsystems with Whitespace in the Name:


blocks = slQuery('SubSystem[Name~=\w+\s+\w+, TreatAsAtomicUnit=on]');
% Replace whitespace in block names with underscore "_"
names = regexprep(strtrim(blocks.Name), '\s+', '_')';
ans = 7×1 cell array
    {'Air_Data_Computer'                }
    {'Log_Hydraulic_System_1_Sensors'   }
    {'Log_Hydraulic_System_2_Sensors'   }


blocks = find_system(get_param(bdroot, 'Handle'), 'RegExp', 'on', ...
                   'BlockType', 'SubSystem', 'TreatAsAtomicUnit', 'on', ...
                   'Name', '\w+\s+\w+');
% Replace whitespace in block names with underscore "_"
names = regexprep(strtrim(get(blocks, 'Name')), '\s+', '_');
ans = 7×1 cell array
    {'Air_Data_Computer'                }
    {'Log_Hydraulic_System_1_Sensors'   }
    {'Log_Hydraulic_System_2_Sensors'   }

The standard API uses the addition of functional arguements to build a query, while slQuery uses a custom query syntax.

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_system and Simulink.findBlocks does not offer.

Find Inports directly connected to a Gain block

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.

r = slQuery('#vdp / Integrator >> $1') ;
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 slQuery features and appeal to my nature.

What Are Your Impressions of This Capability?

Let us know here.

Add A Comment

Your email address will not be published. Required fields are marked *

Preview: hide