Ganesh H
Ganesh H

Reputation: 45

How to get the order of Simulink blocks based on their pin connection?

For Example: From attached Simulink model figure (This model has no meaning. created only to give idea about my problem ), I want to get the order of blocks based on pin connection instead of their alphabetic order.

The order should be like (Expected Output):

  1. Integrator,Second-Order Limited Block

  2. Integrator or Rate Limiter Dynamic Block

  3. Rate Limiter Dynamic or Integrator Block

  4. Lookup table Dynamic Block

  5. Data type Conversion block

  6. n-DLookup Table Block

But Currently I am getting order based on Aplhabatic name of Blocks (Refer below image for the results uisng 'find_system' command.)

Simulink model sample

Current order

Upvotes: 3

Views: 1089

Answers (1)

Phil Goddard
Phil Goddard

Reputation: 10762

There isn't a function that will do this automatically for you - you'll need to write your own.

It looks like you are positioning based on block position (from left to right). In that case, you want something like the following (using the demo f14 model):

>> open_system('f14')
>> blocks = find_system(gcs,'SearchDepth',1,'Type','Block');
>> positions = get_param(blocks,'Position');
>> leftPos = cellfun(@(c)c(1),positions);
>> [~,newOrderIdx] = sort(leftPos);
>> blocks(newOrderIdx)
ans =
  18×1 cell array
    'f14/Pilot'
    'f14/u'
    'f14/Controller'
    'f14/Sum1'
    'f14/Dryden Wind…'
    'f14/Stick Input'
    'f14/Gain'
    'f14/Gain1'
    'f14/Gain2'
    'f14/Actuator…'
    'f14/Sum'
    'f14/Aircraft…'
    'f14/Nz pilot…'
    'f14/Gain5'
    'f14/Angle of …'
    'f14/Pilot G force…'
    'f14/Nz Pilot (g)'
    'f14/alpha (rad)'

Upvotes: 2

Related Questions