3

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

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
Ganesh H
  • 45
  • 6

1 Answers1

2

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)'
Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • 1
    as far as I understood him, this is not what he wanted. He wanted the blocks to be sorted in the order that the signals pass them. He called it 'based on pin connection'. So in his example the first block could be one of the two Integrators (no matter which) but not any other block. – Max Feb 18 '17 at 20:29
  • I agree that "based on pin connection" is a bit vague, and what you describe may be what is required. Nonetheless, the basic idea of my answer holds - that is, a property of the blocks or ports needs to be determined and the blocks sorted based on that property. If it is ports locations or connections that are required then that info is obtained in a similar way to that done above. – Phil Goddard Feb 18 '17 at 20:42
  • @PhilGoddard Above code works fine when the user draw the diagram by placing the blocks according to their pin connection. But as mentioned by Max, I am looking to find the order if user place the blocks in random order. As of now I am trying to come up with a logic using 'block hadles' and 'Port connectivity' to make the solution more robust. – Ganesh H Feb 20 '17 at 11:25