Skip to Main Content Skip to Search
Product Documentation

Controlling and Displaying the Sorted Order

What Is Sorted Order?

During the updating phase of simulation, Simulink determines the order in which to invoke the block methods during simulation. This block invocation ordering is the sorted order.

You cannot set this order, but you can assign priorities to nonvirtual blocks to indicate to Simulink their execution order relative to other blocks. Simulink always honors block priority settings, unless there is a conflict with data dependencies. To confirm the results of priorities that you have set, or to debug your model, display and review the sorted order of your nonvirtual blocks and subsystems.

Displaying the Sorted Order

To display the sorted order of the vdp demo model:

  1. Open the van der Pol equation demo model:

    vdp
  2. In the model window, select Format > Block Displays > Sorted Order.

Simulink displays a notation in the top-right corner of each nonvirtual block and each nonvirtual subsystem. These numbers indicate the order in which the blocks execute. The first block to execute has a sorted order of 0.

For example, in the van der Pol equation model, the Integrator block with the sorted order 0:0 executes first. The Out1 block, with the sorted order 0:1, executes second. Similarly, the remaining blocks execute in numeric order from 0:2 to 0:8.

You can save the sorted order setting with your model. To display the sorted order when you reopen the model, select Edit > Update diagram.

Sorted Order Notation

The sorted order notation varies depending on the type of block. The following table summarizes the different formats of sorted order notation. Each format is described in detail in the sections that follows the table.

Block TypeSorted Order NotationDescription
Nonvirtual Blockss:b
  • s is the system index of the model or subsystem. For root-level models, s is always 0.

  • b specifies the block position within the sorted order for the designated execution context.

Nonvirtual Subsystemss:b{ si}
  • s is the system index of the model or subsystem.

  • b specifies the block position within the sorted order for the designated execution context.

  • si is the subsystem index.

Virtual Blocks and SubsystemsNot applicableVirtual blocks do not execute.
Function-Call Subsystems

One initiator:

s:F{ si}

  • s is the system index of the model or subsystem.

  • F indicates a function-call subsystem.

  • si is the subsystem index.

Two or more initiators:

  • s:F{ si} when the initiators all execute at the same level of the model hierarchy

  • M:F{si}, when the initiators execute at different levels of the model hierarchy

  • s is the system index of the initiators.

  • F indicates a function-call subsystem.

  • si is the subsystem index.

  • M indicates that the initiators execute at different levels of the model hierarchy.

Function-Call Split Blockss:Bb
  • s is a system index of the model or subsystem.

  • B indicates a branch of the function-call signal.

  • b specifies the index of each subsystem connected to each branch of the given function-call signal.

Bus-Capable Blockss:B
  • s is a system index of the model or subsystem.

  • B indicates a bus-capable block.

Nonvirtual Blocks

In the van der Pol equation model, all the nonvirtual blocks in the model have a sorted order. The system index for the top-level model is 0, and the block execution order ranges from 0 to 8.

Nonvirtual Subsystems

The following model contains an atomic, nonvirtual subsystem named Discrete Cruise Controller.

When you enable the sorted order display for the root-level system, Simulink displays the sorted order of the blocks.

The Scope block in this model has the lowest sorted order, but its input depends on the output of the Car Dynamics subsystem. The Car Dynamics subsystem is virtual, so it does not have a sorted order and does not execute as an atomic unit. However, the blocks within the subsystem execute at the root level, so the Integrator block in the Car Dynamics subsystem executes first. The Integrator block sends its output to the Scope block in the root-level model, which executes second.

The Discrete Cruise Controller subsystem has a sorted order of 0:4{1}:

The sorted order of each block inside the Discrete Cruise Controller subsystem has the form 1:b, where:

Virtual Blocks and Subsystems

Virtual blocks, such as the Mux block, exist only graphically and do not execute. Consequently, they are not part of the sorted order and do not display any sorted order notation.

Virtual subsystems do not execute as a unit, and like a virtual block, are not part of the sorted order. The blocks inside the virtual subsystem are part of the root-level system sorted order, and therefore share the system index.

In the model in Nonvirtual Subsystems, the virtual subsystem Car Dynamics does not have a sorted order. However, the blocks inside the subsystem have a sorted order in the execution context of the root-level model. The blocks have the same system index as the root-level model. The Integrator block inside the Car Dynamics subsystem has a sorted order of 0:0, indicating that the Integrator block is the first block executed in the context of the top-level model.

Function-Call Subsystems

Single Initiator.  For each function-call subsystem or model attached to a single initiator, Simulink assigns the block position F. The function-call subsystem (or model) executes when the initiator invokes the function-call subsystem (or model) and, therefore, does not have a sorted order independent of its initiator. Specifically, for a subsystem that connects to one initiator, Simulink uses the notation s:F{ si}, where s is the index of the system that contains the initiator.

For example, the sorted order for the ThresholdCalculation subsystem is 1:F{2}:

Multiple Initiators.  For a function-call subsystem that connects to more than one initiator, the sorted order notation depends on the execution context of the initiators:

For example, open the sl_subsysm_fcncall6 demo model. The f subsystem has three initiators, two from the Stateflow chart, Chart1, and one from the Stateflow chart, Chart.

Because Chart1 has a system index of 2 and Chart has a system index of 1, the initiators execute at different levels in the model hierarchy. The Function-Call Subsystem f has a sorted order notation of M:F{3}.

Function-Call Split Blocks

When a function-call signal is branched using a Function-Call Split block, Simulink displays the order in which subsystems (or models) that connect to the branches execute when the initiator invokes the function call. Simulink uses the notation s:Bb to indicate this order, where B stands for branch. The block position b ranges from 0 to one less than the total number of subsystems (or models) that connect to branches (B0, B1, etc.). When the function-call is invoked, the subsystems execute in ascending order based on this number.

For example, open the sl_subsys_fcncall11 demo model and display the sorted order. The sorted order indicates that the subsystem f (B0) executes before the subsystem g (B1).

Bus-Capable Blocks

A bus-capable block does not execute as a unit and therefore does not have a unique sorted order. Such a block displays its sorted order as s:B where B stands for bus.

For example, open the sldemo_bus_arrays demo model and display the sorted order. Open the For Each Subsystem to see that the sorted order for the Bus Assignment block appears as 1:B.

For more information, see Bus-Capable Blocks.

How Simulink Determines the Sorted Order

Direct-Feedthrough Ports Impact on Sorted Order

To ensure that the sorted order reflects data dependencies among blocks, Simulink categorizes block input ports according to the dependency of the block outputs on the block input ports. An input port whose current value determines the current value of one of the block outputs is a direct-feedthrough port. Examples of blocks that have direct-feedthrough ports include:

Examples of blocks that have non-direct-feedthrough inputs:

Rules for Sorting Blocks

To sort blocks, Simulink uses the following rules:

Applying these rules results in the sorted order. Blocks without direct-feedthrough ports appear at the beginning of the list in no particular order. These blocks are followed by blocks with direct-feedthrough ports arranged such that they can supply valid inputs to the blocks which they drive.

The following model, from Nonvirtual Subsystems, illustrates this result. The following blocks do not have direct-feedthrough and therefore appear at the beginning of the sorted order of the root-level system:

Inside the Discrete Cruise Controller subsystem, all the Gain blocks, which have direct-feedthrough ports, run before the Sum block that they drive.

Assigning Block Priorities

You can assign a priority to a nonvirtual block or to an entire subsystem. Higher priority blocks appear before lower priority blocks in the sorted order. The lower the number, the higher the priority.

Assigning Block Priorities Programmatically

To set priorities programmatically, use the command:

set_param(b,'Priority','n')

where

Assigning Block Priorities Interactively

To set the priority of a block or subsystem interactively:

  1. Right-click the block and select Block Properties.

  2. On the General tab, in the Priority field, enter the priority.

Rules for Block Priorities

Simulink honors the block priorities that you specify unless they violate data dependencies. (Block Priority Violations describes situations that cause block property violations.)

In assessing priority assignments, Simulink attempts to create a sorted order such that the priorities for the individual blocks within the root-level system or within a nonvirtual subsystem are honored relative to one another.

Three rules pertain to priorities:

Priorities Are Relative

Priorities are relative; the priority of a block is relative to the priority of the blocks within the same system or subsystem.

For example, suppose you set the following priorities in the Discrete Cruise Controller subsystem in the model in Nonvirtual Subsystems.

BlockPriority
Gain3
Gain12
Gain21

After updating the diagram, the sorted order for the Gain blocks is as follows.

The sorted order values of the Gain, Gain1, and Gain2 blocks reflect the respective priorities assigned: Gain2 has highest priority and executes before Gain1 and Gain; Gain1 has second priority and executes after Gain2; and Gain executes after Gain1. Simulink takes into account the assigned priorities relative to the other blocks in that subsystem.

The Gain blocks are not the first, second, and third blocks to execute. Nor do they have consecutive sorted orders. The sorted order values do not necessarily correspond to the priority values. Simulink arranges the blocks so that their priorities are honored relative to each other.

Priorities Are Hierarchical

In the Car Dynamics virtual subsystem, suppose you set the priorities of the Gain blocks as follows.

BlockPriority
Gain2
Gain11

After updating the diagram, the sorted order for the Gain blocks is as illustrated. With these priorities, Gain1 always executes before Gain.

You can set a priority of 1 to one block in each of the two subsystems because of the hierarchal nature of the subsystems within a model. Simulink never compares the priorities of the blocks in one subsystem to the priorities of blocks in any other subsystem.

For example, consider this model again.

The blocks within the Car Dynamics virtual subsystem are part of the root-level system hierarchy and are part of the root-level sorted order. The Discrete Cruise Controller subsystem has an independent sorted order with the blocks arranged consecutively from 1:0 to 1:7.

Lack of Priority May Not Result in Low Priority

A lack of priority does not necessarily result in a low priority (higher sorting order) for a given block. Blocks that do not have direct-feedthrough ports execute before blocks that have direct-feedthrough ports, regardless of their priority.

If a model has two atomic subsystems, A and B, you can assign priorities of 1 and 2 respectively to A and B. This priority causes all the blocks in A to execute before any of the blocks in B. The blocks within an atomic subsystem execute as a single unit, so the subsystem has its own system index and its own sorted order.

Block Priority Violations

Simulink software honors the block priorities that you specify unless they violate data dependencies. If Simulink is unable to honor a block priority, it displays a Block Priority Violation diagnostic message.

As an example:

  1. Open the sldemo_bounce demo model.

    Notice that the output of the Memory block provides the input to the Coefficient of Restitution Gain block.

  2. Set the priority of the Coefficient of Restitution block to 1, and set the priority of the Memory block to 2.

    Setting these priorities specifies that the Coefficient of Restitution block execute before the Memory block. However, the Coefficient of Restitution block depends on the output of the Memory block, so the priorities you just set violate the data dependencies.

  3. In the model window, enable sorted order by selecting Format > Block displays > Sorted Order.

  4. Select Edit > Update Diagram.

    The block priority violation warning appears in the MATLAB Command Window. The warning includes the priority for the respective blocks:

    Warning: Unable to honor user-specified priorities. 
    'sldemo_bounce/Memory' (pri=[2]) has to execute
    before 'sldemo_bounce/Coefficient of  Restitution'
     (pri=[1]) to satisfy data dependencies 
  5. Remove the priorities from the Coefficient of Restitution and Memory blocks and update the diagram again to see the correct sorted order.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS