| Contents | Index |
| On this page… |
|---|
Build It Yourself or Use the Supplied Model Design Considerations for Defining the States Adding the Power On and Power Off States |
To implement the states yourself, work through the exercises in this section. Otherwise, open the supplied model by entering this command at the MATLAB prompt:
run(docpath(fullfile(docroot,'toolbox','stateflow','gs','examples','Stage2States.mdl')))
The following sections describe the rationale for the hierarchy and decomposition of states in the chart.
Whether or not to use states depends on the control logic you want to implement. You can model two types of control logic: finite state machines and stateless flow charts. Each type is optimized for different applications, as follows:
| Control Logic | Optimized for Modeling |
|---|---|
| Finite state machines | Physical systems that transition between a finite number of operating modes. In Stateflow charts, you represent each mode as a state. |
| Stateless flow charts | Abstract logic patterns — such as if, if-else, and case statements — and iterative loops — such as for, while, and do loops. You represent these logic constructs with connective junctions and transitions in Stateflow charts. No states are required. See Modeling Logic Patterns and Iterative Loops Using Flow Graphs in the Stateflow User's Guide. |
The Air Controller chart is a system that cools a physical plant by transitioning between several modes of operation and, therefore, can be modeled as a finite state machine. In the following sections, you will design the states that model each mode of operation.
States model modes of operation in a physical system. To determine the number and type of states required for your Air Controller chart, you must identify each mode in which the system can operate. Often, a table or grid is helpful for analyzing each mode and determining dependencies between modes.
Analysis of Operating Modes. For Air Controller, the modes of operation are
| Operating Mode | Description | Dependencies |
|---|---|---|
| Power Off | Turns off all power in the control system | No fan can operate when power is off. |
| Power On | Turns on all power in the control system | Zero, one, or two fans can operate when power is on. |
| Fan 1 | Activates Fan 1 | Fan 1 can be active at the same time as Fan 2. When activated, Fan 1 can turn on or off. |
| Fan 1 On | Cycles on Fan 1 | Fan 1 On can be active if Fan 1 is active and power is on. |
| Fan 1 Off | Cycles off Fan 1 | Fan 1 Off can be active if Fan 1 is active, and power is on. |
| Fan 2 | Activates Fan 2 | Fan 2 can be active at the same time as Fan 1. When activated, Fan 2 can turn on or off. |
| Fan 2 On | Cycles on Fan 2 | Fan 2 On can be active if Fan 2 is active and power is on. |
| Fan 2 Off | Cycles off Fan 2 | Fan 2 Off can be active if Fan 2 is active and power is on. |
| Calculate airflow | Calculates a constant value of 0, 1, or 2 to indicate how fast air is flowing. Outputs this value to the Simulink subsystem for selecting a cooling factor. | Calculates the constant value, based on how many fans have cycled on at each time step. |
Number of States to Define. The number of states depends on the number of operating modes to be represented. In Analysis of Operating Modes, you learned that the Air Controller chart has nine operating modes. Therefore, you need to define nine states to model each mode. Here are the names you will assign to the states that represent each operating mode in Implementing the States to Represent Operating Modes:
| State Name | Operating Mode |
|---|---|
| PowerOff | Power Off |
| PowerOn | Power On |
| FAN1 | Fan 1 |
| FAN2 | Fan 2 |
| SpeedValue | Calculate airflow |
| FAN1.On | Fan 1 On |
| FAN1.Off | Fan 1 Off |
| FAN2.On | Fan 2 On |
| FAN2.Off | Fan 2 Off |
Note Notice the use of dot notation to refer to the On and Off states for FAN1 and FAN2. You use namespace dot notation to give objects unique identifiers when they have the same name in different parts of the chart hierarchy. |
Stateflow objects can exist in a hierarchy. For example, states can contain other states — referred to as substates — and, in turn, can be contained by other states — referred to as superstates. You need to determine the hierarchical structure of states you will define for the Air Controller chart. Often, dependencies among states imply a hierarchical relationship — such as parent to child — between the states.
Based on the dependencies described in Analysis of Operating Modes, here is an analysis of state hierarchy for the Air Controller chart:
| Dependent States | Implied Hierarchy |
|---|---|
| FAN1 and FAN2 depend on PowerOn. No fan can operate unless PowerOn is active. | FAN1 and FAN2 should be substates of a PowerOn state. |
| FAN1.On and FAN1.Off depend on Fan1 and PowerOn. FAN1 must be active before it can be cycled on or off. | FAN1 should have two substates, On and Off. In this hierarchical relationship, On and Off will inherit from FAN1 the dependency on PowerOn. |
| FAN2.On and FAN2.Off depend on FAN2 and PowerOn. FAN2 must be active before it can be cycled on or off. | FAN2 should have two substates, On and Off. In this hierarchical relationship, On and Off will inherit from FAN2 the dependency on PowerOn. |
| The state that calculates airflow needs to know how many fans are running at each time step. | The state that calculates airflow should be a substate of PowerOn so it can check the status of FAN1 and FAN2 at the same level of hierarchy. |
The decomposition of a state dictates whether its substates execute exclusively of each other — as exclusive (OR) states — or can be activated at the same time — as parallel (AND) states. No two exclusive (OR) states can ever be active at the same time, while any number of parallel (AND) states can be activated concurrently.
The Air Controller chart requires both types of states. Here is a breakdown of the exclusive (OR) and parallel (AND) states required for the Stateflow chart:
| State | Decomposition | Rationale |
|---|---|---|
| PowerOff, PowerOn | Exclusive (OR) states | The power can never be on and off at the same time. |
| FAN1, FAN2 | Parallel (AND) states | Zero, one, or two fans can operate at the same time, depending on how much cooling is required. |
| FAN1.On, FAN1.Off | Exclusive (OR) states | Fan 1 can never be on and off at the same time. |
| FAN2.On, FAN2.Off | Exclusive (OR) states | Fan 2 can never be on and off at the same time. |
| SpeedValue | Parallel (AND) state | SpeedValue is an observer state that monitors the status of Fan 1 and Fan 2, updating its output based on how many fans are operating at each time step. SpeedValue must be activated at the same time as Fan 1 and Fan 2, but execute last so it can capture the most current status of the fans. |
When you add states to the Air Controller chart, you will work from the top down in the Stateflow hierarchy. As you learned in Determining the Decomposition of States, the PowerOff and PowerOn states are exclusive (OR) states that turn power off and on in the control system. These states are never active at the same time. By default, states are exclusive (OR) states, represented graphically as rectangles with solid borders.
To add PowerOn and PowerOff to your chart, follow these steps:
Open the model Stage1Interface — either the one you created in the previous exercise or the supplied model for stage 1.
To open the supplied model, enter the following command at the MATLAB prompt:
run(docpath(fullfile(docroot,'toolbox','stateflow','gs','examples','Stage1Interface.mdl')))
Save the model as Stage2States in your local work folder.
In Stage2States, double-click the Air Controller block to open the Stateflow chart.
The Stateflow Editor for Air Controller opens on your desktop. Notice the object palette on the left side of the editor window. This palette displays a set of tools for drawing graphical chart objects, including states:
![]()
Left-click the state tool icon:
![]()
Move your pointer into the drawing area.
The pointer changes to a rectangle, the graphical representation of a state.
Click in the upper-left corner of the drawing area to place the state.
The new state appears with a blinking text cursor in its upper-left corner.
At the text cursor, type PowerOn to name the state.
Move your pointer to the lower-right corner of the rectangle so it changes to this symbol:
![]()
Drag the lower-right corner to enlarge the state as shown:

Click the state tool icon again and draw a smaller state named PowerOff at the bottom of the drawing area, like this:

Save the chart by selecting File > Save Model in the Stateflow Editor, but leave the chart open for the next exercise.
In Determining the States to Define, you learned that FAN1, FAN2, and SpeedValue will be represented by parallel (AND) substates of the PowerOn state. Parallel states appear graphically as rectangles with dashed borders.
In this set of exercises, you will learn how to:
Assign parallel decomposition to PowerOn so its substates can be activated concurrently.
Recall that the decomposition of a state determines whether its substates will be exclusive or parallel.
Add parallel substates to a state in the chart.
Set the order of execution for the parallel substates.
Even though parallel states can be activated concurrently, they execute in a sequential order.
Follow these steps:
In the Air Controller chart, right-click inside PowerOn.
A submenu opens, presenting tasks you can perform and properties you can set for the selected state.
In the submenu, select Decomposition > Parallel (AND).
Save the model Stage2States, but leave the chart open for the next exercise.
Follow these steps:
Left-click the state tool icon in the Stateflow Editor and place two states inside the PowerOn state.
Notice the appearance of the states you just added.
The borders of the two states appear as dashed lines, indicating that they are parallel states. Note also that the substates display numbers in their upper-right corners. These numbers specify the order of execution. Although multiple parallel (AND) states in the same chart are activated concurrently, the chart must determine when to execute each one during simulation.
Name the new substates FAN1 and FAN2.
You have created hierarchy in the Air Controller chart. PowerOn is now a superstate while FAN1 and FAN2 are substates. Your chart should look something like this:

Note Your chart might not show the same execution order for parallel substates FAN1 and FAN2. The reason is that, by default, Stateflow software orders parallel states based on order of creation. If you add FAN2 before FAN1 in your chart, FAN2 moves to the top of the order. You will fine-tune order of activation in a later exercise, Setting Explicit Ordering of Parallel States. |
Tip If you want to move a state together with its substates — and any other graphical objects it contains — double-click the state. It turns gray, indicating that the state is grouped with the objects inside it and that they can be moved as a unit. To ungroup the objects, double-click the state again. |
Save the model Stage2States, but leave the chart open for the next exercise.
Recall that SpeedValue acts as an observer state, which monitors the status of the FAN1 and FAN2 states. To add the SpeedValue state, follow these steps:
Add another substate to PowerOn under FAN1 and FAN2, either by using the state tool icon or copying an existing state in the chart.
You might need to resize the substate to prevent overlap with other substates, but remain within the borders of PowerOn.
Name the state SpeedValue.
Like FAN1 and FAN2, SpeedValue appears as a parallel substate because its parent, the superstate PowerOn, has parallel decomposition.

Save the model Stage2States, but leave the chart open for the next exercise, Setting Explicit Ordering of Parallel States.
Recall that, by default, Stateflow software assigns execution order of parallel states based on order of creation in the chart. This behavior is called explicit ordering. In this exercise, you will set the execution order explicitly for each parallel state in your chart.
In the Stateflow Editor, select File > Chart Properties.
In the Chart properties dialog box, verify that the check box User specified state/transition execution order is selected and click OK.

Note This option also lets you explicitly specify the order in which transitions execute when there is a choice of transitions to take from one state to another. This behavior does not apply to the Air Controller chart because it is deterministic: for each exclusive (OR) state, there is one and only one transition to a next exclusive (OR) state. You will learn more about transitions in Drawing the Transitions Between States. |
Assign order of execution for each parallel state in the Air Controller chart:
Right-click inside each parallel state to bring up its state properties submenu.
From the submenu, select Execution Order and make these assignments:
| For State: | Assign: |
|---|---|
| FAN1 | 1 |
| FAN2 | 2 |
| SpeedValue | 3 |
Here is the rationale for this order of execution:
FAN1 should execute first because it cycles on at a lower temperature than FAN2.
SpeedValue should execute last so it can observe the most current status of FAN1 and FAN2.
Save the model Stage2States, but leave the chart open for the next exercise, Adding the On and Off States for the Fans.
In this exercise, you will enter the on and off substates for each fan. Because fans cannot cycle on and off at the same time, these states must be exclusive, not parallel. Even though FAN1 and FAN2 are parallel states, their decomposition is exclusive (OR) by default. As a result, any substate that you add to FAN1 or FAN2 will be an exclusive (OR) state.
Follow these steps:
Add two substates inside FAN1 and FAN2.
Resize the substates to fit within the borders of FAN1 and FAN2.
In each fan state, name one substate On and name the other Off.
Your Air Controller chart should now look something like this:

Save the model Stage2States.
Where to go next. Now you are ready to specify the actions that execute when a state is active. See Implementing State Actions.
![]() | Defining the States for Modeling Each Mode of Operation | Defining State Actions and Variables | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |