Stateflow Previous page   Next Page

Run a Simulation

These steps show how to run a simulation:

  1. Double-click the On_off Stateflow block to display the Stateflow diagram.
  2. Select Open Simulation Target from the graphics editor Tools menu.

  1. The Simulation Target Builder dialog box appears.

  1. Select Coder Options on the Simulation Target Builder dialog box.

  1. The Simulation Coder Options dialog box appears.

  1. Ensure that the check box to Enable Debugging/Animation is selected.
  2. Select OK to apply the change and close the Simulation Coder Options dialog box.
  3. Close the Simulation Target Builder dialog box.
  4. Select Debug from the graphics editor Tools menu.

  1. The Stateflow Debugging dialog box appears.

  1. Ensure that the Enabled radio button in the Animation section is selected.
  2. Select Close to apply the change and close the window.
  3. Choose Start from the diagram editor Simulation menu to start a simulation of the model and notice the following:
  4. Toggle the on/off switch manual switch (double click it) between its inputs, 0 and 1.

  1. Toggling the on/off switch sends on_switch and off_switch events to the Stateflow chart. These events toggle the chart's active state from Off to On and back to Off again.

    Both the on_switch and off_switch events are defined as Input from Simulink events. This means that the events are input to the chart from Simulink, in this case, from a manual switch. When the input to the on/off switch rises from 0 to 1, this sends an on_switch event to the On_Off chart because on_switch is defined as a rising trigger. Similarly, when the input to the on/off switch falls from 1 to 0, this sends an off_switch event to the chart because the off_switch event is defined as a falling trigger.

  1. Toggle the value of temp switch to change the value of the data temp from 20 to 40.
  2. Toggle the on/off switch again and notice the difference.

  1. Because of the different value for temp, the transition from the Off state to the On state no longer takes place. The transition from Off to On passes through a junction which has an alternate path back to Off. Because the alternate path has a condition, it gets priority. In this case, the condition evaluates to true (nonzero) and the alternate path is taken.

  1. Choose Stop from the graphics editor Simulation menu to stop a simulation.

  1. Once the simulation stops, Stateflow resets the model to be editable.

Where Stateflow Generates Code for Simulation

When you simulate a Simulink model containing Stateflow charts (as you did in the previous section), Stateflow generates a Simulink S-function (sfun) target that enables Simulink to simulate the Stateflow blocks. The sfun target can be used only with Simulink.

Before generating code, Stateflow creates a directory called sfprj in the current MATLAB directory if the directory does not already exist. Stateflow uses the sfprj directory during code generation to store generated files. See the section Generated Files.

If you have the Stateflow Coder, you can generate stand-alone code suitable for a particular processor. See Building a Target for more information on code generation.


Previous page  Parse the Stateflow Diagram Debug the Model During Simulation Next page

 © 1994-2010 The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS