File Exchange Pick of the Week

Our best user submissions

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 find_system function. A more recent addition is the Simulink.findBlocks and Simulink.findBlocksOfType functions which provide the same functionality as find_system and are more robust (for example: making sure to locate inactive variants and commented blocks as well)

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:

slQuery

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'   }

Simulink.findBlocksOfType

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 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

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 slQuery features and appeal to my nature.

What Are Your Impressions of This Capability?

Let us know here.

|
  • print

コメント

コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。