Documentation Center

  • Trial Software
  • Product Updates

Activate a Simulink Block Using Output Events

What Is an Output Event?

An output event is an event that occurs in a Stateflow® chart but is visible in Simulink® blocks outside the chart. This type of event allows a chart to notify other blocks in a model about events that occur in the chart.

You use output events to activate other blocks in the same model. You can define multiple output events in a chart, where each output event maps to an output port (see Port).

    Note:   Output events must be scalar.

Activate a Simulink Block Using Edge Triggers

An edge-triggered output event activates a Simulink block to execute during the current time step of simulation. This type of output event works only when a change in control signal acts as a trigger.

When to Use an Edge-Triggered Output Event

Use an edge-triggered output event to activate a Simulink subsystem when your model requires subsystem execution at regular (or periodic) intervals.

How to Define an Edge-Triggered Output Event

To define an edge-triggered output event:

  1. Add an event to the Stateflow chart, as described in Define Events.

  2. Set the Scope property for the event to Output to Simulink.

    For each output event you define, an output port appears on the Stateflow block.

  3. Set the Trigger property of the output event to Either Edge.

      Note:   Unlike edge-triggered input events, you cannot specify a Rising or Falling edge trigger.

Example of Using an Edge-Triggered Output Event

The following modelmodel shows how to use an edge-triggered output event to activate triggered subsystems at regular intervals.

The chart contains the edge-triggered output event e1 and the local data a, which switches between 0 and 1 during simulation.

In a chart, the Trigger property of an edge-triggered output event is always Either Edge. However, Simulink triggered subsystems can have a Rising, Falling, or Either Edge trigger. This model shows the difference between triggering a rising edge subsystem and an either edge subsystem.

The output event e1 triggers the...On...When the data a switches...
Either edge subsystemEvery event broadcastFrom 0 to 1, or from 1 to 0
Rising edge subsystemEvery other event broadcastFrom 0 to 1

When you simulate the model, the scope shows these results.

Queuing Behavior for Broadcasting an Edge-Triggered Output Event Multiple Times

If a chart tries to broadcast the same edge-triggered output event multiple times in a single time step, the chart dispatches only one of these broadcasts in the present time step. However, the chart queues up any pending broadcasts for dispatch — that is, one at a time in successive time steps. Each time the chart wakes up in successive time steps, if any pending broadcasts exist for the output event, the chart signals the edge-triggered subsystem for execution. Based on the block sorted order of the Simulink model, the edge-triggered subsystem executes. (For details, see Control and Display the Sorted Order in the Simulink documentation.)

Queue Edge-Triggered Output Event Broadcasts

In this modelmodel, the chart Caller uses the edge-triggered output event output_cmd to activate the chart Callee.

The chart Caller tries to broadcast the same edge-triggered output event four times in a single time step, as shown.

Each time the chart Callee is activated, the output data y increments by one.

When you simulate the model, you see this output in the scope.

At t = 1, the chart Caller dispatches only one of the four output events. Therefore, the chart Callee executes once during that time step. However, the chart Caller queues up the other three event broadcasts for future dispatch — that is, one at a time for t = 2, 3, and 4. Each time Caller wakes up in successive time steps, it activates Callee for execution. Therefore, the action y++ occurs once per time step at t = 1, 2, 3, and 4. During simulation, Callee executes based on the block sorted order of the Simulink model.

Approximate a Function Call Using Queuing Behavior

When you cannot use a function-call output event, such as for HDL code generation, you can approximate a function call by using:

  • An edge-triggered output event

  • An enabled subsystem

  • Two consecutive event broadcasts

    Note:   While you can approximate a function call, a subtle difference in execution behavior exists. Execution of a function-call subsystem occurs during execution of the chart action that provides the trigger. However, execution of an enabled subsystem occurs after execution of that chart action is complete.

In the following modelmodel, the chart Caller uses the edge-triggered output event output_cmd to activate the enabled subsystem. The scope shows the value of the output event during simulation.

The chart Caller broadcasts the edge-triggered output event using a send action.

When you simulate the model, you see the following output in the scope. The simulation runs for 40 seconds.

When simulation starts, the value of output_cmd is 0. At t = 20, the chart dispatches output_cmd. Because this value changes to 1, the enabled subsystem becomes active and executes during that time step. Because no other event broadcasts occur, the enabled subsystem continues to execute at every time step until simulation ends. Therefore, the Display block shows a final value of 40.

To approximate a function call, add a second event broadcast in the same action.

When you simulate the model, you see the following output in the scope. The simulation runs for 40 seconds.

When simulation starts, the value of output_cmd is 0. At t = 20, the chart dispatches output_cmd. Because this value changes to 1, the enabled subsystem becomes active and executes during that time step. The chart also queues up the second event for dispatch at the next time step. At t = 21, the chart dispatches the second output event, which changes the value of output_cmd to 0. Therefore, the enabled subsystem stops executing and the Display block shows a final value of 20.

The queuing behavior of consecutive edge-triggered output events enables you to approximate a function call with an enabled subsystem.

Activate a Simulink Block Using Function Calls

A function-call output event activates a Simulink block to execute during the current time step of simulation. This type of output event works only on blocks that you can trigger with a function call.

When to Use a Function-Call Output Event

Use a function-call output event to activate a Simulink block when your model requires access to output data from the block in the same time step as the function call.

How to Define a Function-Call Output Event

To define a function-call output event:

  1. Add an event to the chart, as described in Define Events.

  2. Set the Scope property for the event to Output to Simulink.

    For each output event you define, an output port appears on the Stateflow block.

  3. Set the Scope property of the output event to Function call.

Example of Using a Function-Call Output Event

The model sf_loop_schedulersf_loop_scheduler shows how to use a function-call output event to activate a Simulink block. For information on running this model and how it works, see Schedule One Subsystem in a Single Step.

The function-call output event...Of the chart...Activates...
callEdge to FunctionThe chart Looping Scheduler
A1Looping SchedulerThe function-call subsystem A1

Interleaving Behavior for Broadcasting a Function-Call Output Event Multiple Times

If a chart tries to broadcast the same function-call output event multiple times in a single time step, the chart dispatches all the broadcasts in that time step. Execution of function-call subsystems is interleaved with the execution of the function-call initiator so that output from the function-call subsystem is available right away in the function-call initiator. (For details, see Create a Function-Call Subsystem in the Simulink documentation.)

Interleave Function-Call Output Event Broadcasts

In this modelmodel, the chart Caller uses the function-call output event output_cmd to activate the chart Callee.

The chart Caller tries to broadcast the same function-call output event four times in a single time step, as shown.

Each time the chart Callee is activated, the output data y increments by one.

When you simulate the model, you see this output in the scope.

At t = 1, the chart Caller dispatches all four output events. Therefore, the chart Callee executes four times during that time step. Therefore, the action y++ also occurs four times in that time step. During simulation, execution of Callee is interleaved with execution of Caller so that output from Callee is available right away.

Association of Output Events with Output Ports

The Port property associates an output event with an output port on the chart block that owns the event. This property specifies the position of the output port relative to others.

All output ports appear sequentially from top to bottom. Output data ports appear sequentially above output event ports on the right side of a chart block. As you add output events, their default Port properties appear sequentially at the end of the current port list.

You can change the default port assignment of an event by resetting its Port property. When you change the Port property for an output event, the ports for the remaining output events automatically renumber, preserving the original order. For example, assume you have three output events, OE1, OE2, and OE3, which associate with the output ports 4, 5, and 6, respectively. If you change the Port property for OE2 to 6, the ports for OE1 and OE3 renumber to 4 and 5, respectively.

Access Simulink Subsystems Triggered By Output Events

To access the Simulink subsystem associated with a Stateflow output event:

  1. In your chart, right-click the state or transition that contains the event of interest and select Explore.

  2. Select the desired event.

    The Simulink subsystem associated with the event appears.

Was this topic helpful?