Model Finite State Machines by Using Stateflow Charts
A finite state machine is a representation of an event-driven, reactive system that transitions from one operating mode to another when the condition defining the change is true. For example, you can use a state machine to represent the automatic transmission of a car. The transmission has operating modes, such as park, reverse, neutral, drive, and low. As the driver moves the gearshift, the system transitions from one operating mode to another.
Types of Stateflow Blocks
To represent the relationships between the inputs, outputs, and operating modes of a finite state machine, you can add Stateflow® blocks to a Simulink® model to create state transition diagrams, state transition tables, and truth tables:
A Chart is a graphical representation of a finite state machine based on a state transition diagram. In a Stateflow chart, states and transitions form the basic building blocks of a sequential logic system. States correspond to operating modes and transitions represent pathways between states. For more information, see Represent Operating Modes by Using States and Transition Between Operating Modes.
A State Transition Table represents a finite state machine for sequential modal logic in tabular format. Instead of drawing states and transitions in a Stateflow chart, you can use a state transition table to model a state machine in a concise, compact format that requires minimal maintenance of graphical objects. For more information, see Use State Transition Tables to Express Sequential Logic in Tabular Form.
A Truth Table implements combinatorial logic design in a tabular format. You can use truth table blocks to model decision making for fault detection and management and mode switching. For more information, see Use Truth Tables to Model Combinatorial Logic.
To implement control logic, Stateflow charts and State Transition Table blocks can use MATLAB® or C as the action language. Truth Table blocks use only MATLAB as the action language. For more information, see Differences Between MATLAB and C as Action Language Syntax.
To combine the advantages of state machine programming with the full functionality of MATLAB, you can create a standalone Stateflow chart. You execute standalone charts as MATLAB objects directly through the Command Window or by using a script. You can also program a MATLAB app that controls the state of the chart through a graphical user interface. For more information, see Create Stateflow Charts for Execution as MATLAB Objects.
Program a Stateflow Chart
To create a Stateflow chart that models a finite state machine:
Create a Simulink model that contains an empty Stateflow chart by calling the function
To open the Stateflow Editor, double-click the chart block. For more information on using the Stateflow Editor, see Stateflow Editor Operations.
For each operating mode in your system, draw a state and implement the state actions by adding state labels, as described in Represent Operating Modes by Using States.
To organize complex systems, define a hierarchy of states by drawing child states inside a parent state. For example, you can use a superstate to enclose substates that share the same state actions. For more information, see Use State Hierarchy to Design Multilevel State Complexity.
To model operating modes that are active at the same time, enable parallel (AND) decomposition in a parent state. For more information, see Define Exclusive and Parallel Modes by Using State Decomposition.
To represent the direction of flow logic between states, draw transitions and implement the transition conditions by adding transition labels, as described in Transition Between Operating Modes.
To mark the first state to become active, use a default transition. For more information, see Use Default Transitions to Specify Initial Substate Activity.
To create paths from a single source to multiple destinations or from multiple sources to a single destination, combine transitions and connective junctions. For more information, see Combine Transitions and Junctions to Create Branching Paths.
If your system has inputs or outputs, or depends on any state variables, add input, output, and local data, as described in Add Stateflow Data.
If your system reacts to event triggers or must trigger actions in your chart or other blocks in your model, add input, output, or local events, as described in Synchronize Model Components by Broadcasting Events.
If your chart has complex state actions or transition conditions, add reusable functions to your chart. Use the function format that is most natural for the type of calculation in the state action or transition condition by selecting from these functions:
Graphical functions — Encapsulate flow charts that contain logic and iterative loop patterns. See Reuse Logic Patterns by Defining Graphical Functions.
MATLAB functions — Write matrix-oriented algorithms for data analysis and visualization. See Reuse MATLAB Code by Defining MATLAB Functions.
Simulink functions — Streamline your design by calling Simulink function-call subsystems. See Reuse Simulink Functions in Stateflow Charts.
Truth tables — Represent combinational logic for decision-making applications. See Use Truth Tables to Model Combinatorial Logic.
Alternatively, you can write your own C or C++ code for integration with your chart. For more information, see Reuse Custom Code in Stateflow Charts.
Connect the chart to other blocks in the Simulink model by using input and output ports.
To simulate the model, click Run . During the simulation, the Stateflow Editor highlights active states and transitions through chart animation.
For a tutorial that illustrates this workflow, see Construct and Run a Stateflow Chart.
 Harel, David. "Statecharts: A Visual Formalism for Complex Systems." Science of Computer Programming 8, no.3 (June 1987): 231-74.
 Hatley, Derek J. and Imtiaz A. Pirbhai. Strategies for Real-Time System Specification. New York, NY: Dorset House Publishing, 1988.