Model Reactive Systems in Stateflow

Identify System Attributes

Before you build a Stateflow® chart in a Simulink® model, identify your system attributes by answering these questions:

  1. What are your interfaces?

    1. What are the event triggers to which your system reacts?

    2. What are the inputs to your system?

    3. What are the outputs from your system?

  2. Does your system have any operating modes?

    1. If the answer is yes, what are the operating modes?

    2. 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.

Select a State Machine Type

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.

For more information, see How Stateflow Objects Interact During Execution, Differences Between MATLAB and C as Action Language Syntax, and Overview of Mealy and Moore Machines.

Specify State Actions and Transition Conditions

After you create an empty chart, answer the following questions:

  1. For each state, what are the actions you want to perform?

  2. 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:

  1. Draw states to represent your operating modes, if any. See Represent Operating Modes by Using States.

  2. Implement the state actions by adding state labels that use the appropriate syntax. See State Action Types.

  3. Draw transitions to represent the direction of flow logic, between states or between branches of your flow chart. See Transition Between Operating Modes.

  4. Implement the transition conditions by adding transition labels that use the appropriate syntax. See Transition Action Types.

Define Persistent Data to Store State Variables

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:

  1. Add local data to the appropriate level of the chart hierarchy. See Add Stateflow Data.

    You can also use the Symbol Wizard to add data to your chart. See Resolve Symbols Through the Symbol Wizard.

  2. Specify the type, size, complexity, and other data properties. See Set Data Properties.

Simplify State Actions and Transition Conditions with Function Calls

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.

Check That Your System Representation Is Complete

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.