| Contents | Index |
| On this page… |
|---|
Specify State Actions and Transition Conditions Define Persistent Data to Store State Variables Simplify State Actions and Transition Conditions with Function Calls |
Before you build the state chart, identify your system attributes by answering these questions:
What are your interfaces?
What are the event triggers to which your system reacts?
What are the inputs to your system?
What are the outputs from your system?
Does your system have any operating modes?
If the answer is yes, what are the operating modes?
Between which modes can you transition? Are there any operating modes that run in parallel?
If your system has no operating modes, the system is stateless. If your system has operating modes, the system is modal.
After identifying your system attributes, the first step is to create a new chart and select one of the following state machine types:
Classic — The default machine type. Provides the full set of Stateflow chart semantics.
Mealy — Machine type in which output is a function of inputs and state.
Moore — Machine type in which output is a function of state.
For more information about Classic charts, see Stateflow Chart Semantics. For more information about Mealy and Moore charts, see Building Mealy and Moore Charts.
After you create an empty chart, answer the following questions:
For each state, what are the actions you want to perform?
What are the rules for transitioning between your states? If your chart has no states, what are the rules for transitioning between branches of your flow logic?
Using your answers to those questions, specify state actions and transition conditions:
Draw states to represent your operating modes, if any. See Working with States in Charts.
Implement the state actions by adding state labels that use the appropriate syntax. See State Action Types.
Draw transitions to represent the direction of flow logic, between states or between branches of your flow graph. See Working with Transitions in Charts.
Implement the transition conditions by adding transition labels that use the appropriate syntax. See Transition Action Types.
After adding state actions and transition conditions to your chart, determine if the chart requires any local or persistent data to store state variables. If so, follow these steps:
Add local data to the appropriate level of the chart hierarchy. See Adding Data.
Specify the type, size, complexity, and other data properties. See Setting Data Properties in the Data Dialog Box.
State actions and transition conditions can be complex enough that defining them in-line on the state or transition is not feasible. In this case, express the actions or conditions using one of the following types of Stateflow functions:
Flow graph — Encapsulate flow graphs containing if-then-else, switch-case, for, while, or do-while patterns.
MATLAB — Write matrix-oriented algorithms; call MATLAB functions for data analysis and visualization.
Simulink — Call Simulink function-call subsystems directly to streamline design and improve readability.
Truth table — Represent combinational logic for decision-making applications such as fault detection and mode switching.
Use the function format that is most natural for the type of calculation in the state action or transition condition. For more information on the four types of functions, see:
If the four types of Stateflow functions do not work, you can write your own C or C++ code for integration with your state chart. For more information about custom code integration, see Building Targets.
Does your Stateflow chart fully express the logical or event-driven components of your system?
If the answer is yes, you are done.
If the answer is no, you can create a separate chart or add hierarchy to your current chart.
To create a new chart, repeat all the steps in this basic workflow.
To add hierarchy, repeat the previous three steps on lower levels of the current chart.
![]() | Creating Stateflow Charts | Creating an Empty State Chart | ![]() |

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 |