Scheduling Subsystems in Stateflow
This example shows three different methods for scheduling subsystem execution in Stateflow®. This model covers three different scenarios:
- Scheduling multiple subsystems in a single step 
- Scheduling a subsystem multiple times in a single step 
- Scheduling multiple subsystems to execute at specific times 
For each scenario, you can use a different type of Stateflow logic to manage the execution of your subsystems. To schedule multiple subsystems in a single step, use ladder logic. The ladder logic scheduler design pattern allows you to specify the order in which multiple Simulink® subsystems execute in a single time step. To schedule a subsystem multiple times in a single step, use loop logic. The loop scheduler design pattern allows you to execute a Simulink subsystem multiple times in a single time step. To schedule multiple subsystems to execute at specific times, use temporal logic. The temporal logic scheduler design pattern allows you to schedule Simulink subsystems to execute at specified times.
In all three models, Stateflow schedulers extend control of subsystem execution in a Simulink model, which determines order of execution implicitly based on block connectivity and sample time propagation.
Scheduling Multiple Subsystems in a Single Step
This portion of the example shows how to design a ladder logic scheduler in Stateflow.
open_system("sf_ladder_logic_scheduler")
The Ladder Logic Scheduler chart broadcasts a series of function-call output events to execute three function-call subsystems (A1, A2, and A3). During each time step:
- The Simulink model activates the - Edge to Functionchart at the rising edge of the 1-millisecond pulse generator.
- The Edge to Function chart broadcasts the function-call output event - callto activate- Ladder Logic Schedulerchart.
- The - Ladder Logic Schedulerchart uses sequencing ladder logic to broadcast function-call output events based on the values of the input signals- u1and- u2.

The chart evaluates each condition sequentially. When a condition is valid, the chart calls the send operator to broadcast an output event. The corresponding subsystem computes its output and returns control back to the Ladder Logic Scheduler chart.
When you simulate the model, the scope shows the input and output of each function-call subsystem. During each time step, the Ladder Logic Scheduler chart executes the subsystems depending on the values of the input signals u1 and u2:
- If - u1is positive, the chart sends a function-call output event to execute subsystem- A1. This subsystem multiplies the value of- u1by a gain of- 3and passes this value back to the Ladder Logic Scheduler chart as input- u2. Control returns to the next condition in the- Ladder Logic Schedulerchart.
- If - u2is greater than- 1, the chart sends a function-call output event to execute subsystem- A2. This subsystem decreases the value of- u2by- 1. Control returns to the final condition in the- Ladder Logic Schedulerchart.
- If - u2is less than- 2, the chart sends a function-call output event to execute subsystem- A3. This subsystem multiplies its input by a gain of- 2.
In the scope, horizontal segments indicate time steps when a subsystem does not execute.
Schedule a Subsystem Multiple Times in a Single Step
This portion of the example shows how to design a loop scheduler in Stateflow.
open_system("sf_loop_scheduler")
The Loop Scheduler chart broadcasts a function-call output event to execute the function-call subsystem A1 multiple times every time step. During each time step:
- The Simulink model activates the - Edge to Functionchart at the rising edge of the 1-millisecond pulse generator.
- The - Edge to Functionchart broadcasts the function-call output event- callto activate the- Loop Schedulerchart.
- The - Loop Schedulerchart calls the- sendoperator to broadcast the function-call output event- A1multiple times.

Each broadcast of the event A1 executes the subsystem A1. The subsystem computes its output and returns control back to the Loop Scheduler chart.
When you simulate the model, the scope displays the value of y at each time step. During each time step, the value of y increases by 25 because:
- The flow chart in the - Loop Schedulerchart implements a- forloop that iterates 10 times.
- In each iteration of the - forloop, the chart increments- yby 1 (the constant value of input- u1).
- Each time that the chart broadcasts the output event to subsystem - A1, the subsystem increments- yby 1.5.

Schedule Subsystems to Execute at Specific Times
This portion of the example shows how to design a temporal logic scheduler in Stateflow.
open_system("sf_temporal_logic_scheduler")
In this example, the Temporal Logic Scheduler chart contains two states that schedule the execution of three function-call subsystems (A1, A2, and A3) at different rates, as determined by the temporal logic operator every.

When the FastScheduler state is active, the chart schedules function calls to different Simulink subsystems at a fraction of the base rate at which the input event call wakes up the chart.
- The chart sends an event to execute subsystem - A1at the base rate.
- The chart sends an event to execute subsystem - A2at half the base rate.
- The chart sends an event to execute subsystem - A3at one quarter the base rate.
When the SlowScheduler state is active, the chart schedules function calls for A1, A2, and A3 at 1/8, 1/16, and 1/32 times the base rate.
The chart switches between the fast and slow execution modes after every 100 invocations of the call event.
When you simulate the model, the scope displays the value of y at each time step. The changes in value illustrate the different rates of execution.
- When the chart executes the subsystems at a slow rate (for example, from - t=4.5to- t=4.6, from- t=4.7to- t=4.8, and from- t=4.9to- t=5), the values change slowly.
- When the chart executes the subsystems at a fast rate (for example, from - t=4.6to- t=4.7and from- t=4.8to- t=4.9), the values change rapidly.
