Main Content

Construct and Run a Stateflow Chart

A Stateflow® chart is a graphical representation of a finite state machine consisting of states, transitions, and data. You can create a Stateflow chart to define how a system reacts to external input signals, events, and time-based conditions.

For instance, this Stateflow chart presents the logic underlying a half-wave rectifier. The chart contains two states labeled On and Off. In the On state, the chart output signal y is equal to the input x. In the Off state, the output signal is set to zero. When the input signal crosses some threshold t0, the chart transitions between these states. The actions in each state update the value of y at each time step of the simulation.

Stateflow chart with two states.

This example shows how to create and simulate this Stateflow chart.

Construct the Stateflow Chart

Open the Stateflow Editor

The Stateflow Editor is a graphical environment for designing state transition diagrams, flow charts, state transition tables, and truth tables.

  1. To open a new Simulink model that contains a Stateflow chart, in the MATLAB Command Window, use the sfnew function:

    sfnew rectify

    Simulink® creates a model called rectify that contains an empty Stateflow Chart block.

  2. To open the Stateflow Editor, double-click the chart block.

The main components of the Stateflow Editor are the chart canvas, the object palette, and the Symbols pane.

  • The chart canvas is a drawing area where you create a chart by combining states, transitions, and other graphical elements.

  • On the left side of the canvas, the object palette displays a set of tools for adding graphical elements to your chart.

  • On the right side of the canvas, in the Symbols pane, you add new data, events, and messages to the chart and resolve any undefined or unused symbols.

Default view of the Stateflow Editor.

Add States and Transitions

  1. From the object palette, click the State icon and move the pointer to the chart canvas. A state with its default transition appears. To place the state, click a location on the canvas. At the text prompt, enter the state name On and the state action y = x.

    Chart with one state, On.

  2. Add another state. Right-click and drag the On state. Blue graphical cues help you to align your states horizontally or vertically. The name of the new state changes to Off. Double-click the state and modify the state action to y = 0.

    Chart with two states, On and Off.

  3. Realign the two states and pause on the space between the two states. Blue transition cues indicate several ways in which you can connect the states. To add transitions, click the appropriate cue.

    Alternatively, to draw a transition, click and drag from the edge of one state to the edge of the other state.

    Chart with the two states joined by transitions.

  4. Double-click each transition and type the appropriate transition condition x<t0 or x>=t0. The conditions appear inside square brackets.

    Chart with transition conditions.

  5. Clean up the chart:

    • To improve clarity, move each transition label to a convenient location above or below its corresponding transition.

    • To align and resize the graphical elements of your chart, in the Format tab, click Auto Arrange or press Ctrl+Shift+A.

    • To resize the chart to fit the canvas, press the space bar or click the Fit To View icon .

Resolve Undefined Symbols

Before you can execute your chart, you must define each symbol that you use in the chart and specify its scope (for example, input data, output data, or local data).

  1. In the Modeling tab, under Design Data, select Symbols Pane.

    In the Symbols pane, undefined symbols are marked with a red error badge .

  2. In the Symbols pane, click Resolve Undefined Symbols . The Type column displays the suggested scope for each undefined symbol based on its usage in the chart. The Stateflow Editor resolves the symbols x and t0 as input data and y as output data .

    Before and after views of the Symbols pane resolving the undefined symbols t0, x, and y.

  3. Because the threshold t0 does not change during simulation, change its scope to constant data. In the Type column, click the data type icon next to t0 and select Constant Data.

  4. Set the value for the threshold t0. In the Value column, click the blank entry next to t0 and enter a value of 0.

  5. Save your Stateflow chart.

Simulate the Model

To simulate the Simulink model, connect the chart block to other blocks in the model through input and output ports.

  1. To return to the Simulink Editor, on the explorer bar at the top of the canvas, click the name of the Simulink model: rectify. If the explorer bar is not visible, click the Hide/Show Explorer Bar icon at the top of the object palette.

  2. Add a source to the model:

    • From the Simulink Sources library, add a Sine Wave (Simulink) block.

    • Double-click the Sine Wave block and set the Sample time to 0.2.

    • Connect the output of the Sine Wave block to the input of the Stateflow chart.

    • Label the signal as x.

  3. Add a sink to the model:

    • From the Simulink Sinks library, add a Scope (Simulink) block with two input ports.

    • Connect the output of the Sine Wave block to the first input of the Scope block.

    • Connect the output of the Stateflow chart to the second input of the Scope block.

    • Label the signal as y.

  4. Save the Simulink model.

    In a Simulink model, a Sine Wave block creates an input signal for the chart. A Scope block plots the input and output of the chart.

  5. To simulate the model, click Run . During the simulation, the Stateflow Editor highlights active states and transitions through chart animation.

  6. After you simulate the model, double-click the Scope block. The scope displays the graphs of the input and output signals to the charts.

    Scope block showing the input and output of the chart.

    The simulation results show that the rectifier filters out negative input values.

Related Topics