Before you build a Stateflow® chart in a Simulink® model, 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.
For more information, see
sfnew. Select one of the following
state machine types:
Classic — The default machine type. Provides the full set of semantics for MATLAB charts and C charts.
Mealy — Machine type in which output is a function of inputs and state.
Moore — Machine type in which output is a function of state.
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 Represent Operating Modes by Using States.
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 chart. See Transition Between Operating Modes.
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:
State actions and transition conditions can be complex enough that defining them inline 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 chart — Encapsulate flow charts 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 chart. For more information about custom code integration, see Reuse Custom Code in Stateflow Charts.
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.