Main Content

Control Output Switch with Event Actions and Simulink Function

This example shows how to change the selected output port of an Entity Output Switch block to route entities along different paths where a path is selected on a per-entity basis, not on a predetermined time schedule.

Control Output Switch with a Simulink Function Block

The following example illustrates a scenario in which the Entity Output Switch block is controlled by the Simulink Function block.

Snapshot of a simple block diagram showing an Entity Server block connected to an Entity Output Switch block. A Simulink Function block named SwitchCtrl() is connected to the input control port of the Entity Output Switch.

  1. Double-click the function signature on the Simulink Function block and enter SwitchCtrl().

  2. Double-click the Simulink Function block. Add a Repeating Sequence Stair block, and set its Sample time parameter to -1 (inherited), a Message Send block and an Out1 block. Connect the blocks as shown.

    Snapshot of the SwitchCtrl() Simulink Function block showing the Repeating Sequence Stair block connected to the Message Send block. The output of the Message Send block connects to an Output Port block.

  3. In the Repeating Sequence Stair block, set the Vector of output values to [3 2 1].

    When the Simulink Function block executes, it outputs the next number from a repeating sequence and Message Send block outputs message values 3, 2, or 1 based on the sequence of values specified in the Repeating Sequence Stair block.

  4. In the Entity Server block, in the Exit action field enter this code.


When service in the Entity Server block is complete, the entity exits the block and advances to the Entity Output Switch block. The departure of the entity from the Entity Server block calls the SwitchCtrl() function which activates the Simulink Function block. Then the output message of the Simulink Function block determines which output port the entity uses when it departs the Entity Output Switch block.

Specify an Initial Port Selection

When the Entity Output Switch block uses an input message, the block might attempt to use the message before its first sample time hit. If the initial value of the message is out of range (for example, it is unavailable). You should then specify the initial port selection in the Entity Output Switch block's dialog box. To achieve this, you can follow these steps.

  1. In the Entity Output Switch, select From control port as the Switching criterion.

  2. Set Initial port selection to the desired initial port. The value must be an integer between 1 and Number of output ports. The Entity Output Switch block uses Initial port selection until the first control port message arrives.

See Also

| | |

Related Examples

More About