Late-Breaking News for the MATLAB 5.2 Product Family     Search    Help Desk 

Chapter 5
Stateflow 1.0.6


Introduction

This chapter provides information about Stateflow 1.0.6, which is being shipped with MATLAB 5.2.

Note: The version of Stateflow (1.0.6) shipped with MATLAB 5.2 is essentially the same as the Patch Release 1.0.5 recently made available via FTP to Stateflow customers. However, this version fixes some bugs that still existed in the patch release.

Product Requirements

Stateflow is a multiplatform product, running on Microsoft Windows 95, Windows NT, and UNIX systems.

Stateflow version 1.0.6 requires:

See the MATLAB Application Program Interface Guide for information on how to ensure proper installation for integration into the MATLAB environment.

In addition, you can optionally use the Real-Time Workshop to generate floating-point code for Simulink portions of the model.

Enhancements

Bug Fixes to Stateflow 1.0

Version 1.0.6 fixes these bugs that were in Version 1.0:

Enhancements to the Sensor Failure Detection Demo

The Sensor Failure Detection demo in Stateflow 1.0.6 includes these enhancements:

Current Restrictions

Supported C Compiler

A C or C++ compiler is required for creating MATLAB MEX-files on your platform. For an up-to-date list of compilers supported by MATLAB, see The MathWorks Technical Support Department's Technical Notes at:

Simulink Libraries and Stateflow Blocks

Stateflow blocks do not currently support Simulink libraries.

Machine Properties Dialog Box

There is a Machine properties dialog box for each machine. Currently this dialog box is accessible only through the Chart properties dialog box. The machine is shown in the Chart properties dialog box as the chart's parent. Clicking on the Parent field hypertext link displays the Machine properties dialog box. You can specify these fields in the Machine properties dialog box:

Stateflow Finder and Case-Sensitive Searches

The Stateflow Finder performs case-sensitive searches based on the string criteria you enter. Currently you cannot specify a case-insensitive search.

Spaces in State Names

Spaces in the state name portion of a state label are not currently supported. For example, if you entered state one as one state's name and state two as another state's name (at the same level in the hierarchy), the parser would generate an error indicating the names are not unique.

Undo Operation

Stateflow does not currently support an undo operation. However, delete operations copy the deleted objects to the paste buffer. You can undo a delete operation by immediately pasting the deleted objects.

Directed Event Broadcast Using send

As described in the Stateflow User's Guide, you can specify a directed event broadcast in the action language. The format of the directed broadcast is:

where event_name is broadcast to state_name and any offspring of that state in the hierarchy.

Currently, you cannot direct the event_name to a state_name in another machine.

Sharing Workspace Data in a Real-Time Workshop Target

Sharing workspace data between two or more Stateflow charts in a Real-Time Workshop (RTW)target can cause a collision when these conditions are present:

It is important to note that all of these conditions must be present to cause a problem. If these conditions exist, you should use an explicit method within Simulink to exchange the workspace data between charts. For more information, see the "Models with Multiple Sample Rates" chapter of the Real-Time Workshop User's Guide.

Real-Time Workshop RealTimeMalloc Code Format Option

The Stateflow Coder does not support the Real-Time Workshop RealTimeMalloc code format option.

Graphics Editor Edit Menu and Text Labels

The Cut, Copy, and Paste menu items on the graphics editor Edit menu operate on Stateflow objects (states, transitions, and junctions). Text, such as a state or transition label, is not considered an object. Currently, when you edit a text label, the Edit menu is disabled during the edit operation.

Known Software Problems

Resizing the Graphics Editor Window

If you resize the graphics editor window by quickly dragging the lower right-hand corner, the window may snap back to its original size. This behavior occurs only on PC platforms when opaque sizing is enabled.

Stateflow Finder Dialog Box Cannot Be Resized

You cannot resize the Finder dialog box in this release.

Stateflow Quick Reference Guide

Graphics Editor

Window Navigation and Zooming:
Esc key- Activate the Simulink window containing the Stateflow diagram
Space bar - Resize the Stateflow diagram to fit within the current window
F key- Zoom in on the selected object
Object Selection:
Left mouse click - Select a single object

Shift key + left mouse clicks - Individually select multiple objects; toggle object selection
Left mouse click + drag - Drag selection rubberband; all objects entirely or partially within the rubberband are selected
Grouping:
Left mouse double click - Group all objects within selected state; toggle grouping on and off

Action Language

Transition Label:
Event_name [condition] {condition_action} / transition_action

State Label:
name/
entry:
during:
exit:
on event_name:

Reserved Words:

(abbreviation)

change (chg)
during (du)
entry (en)

exit (ex)

in

on

send

matlab (ml)

Operators:

+ addition

- subtraction

= assignment

* multiplication

%% modulus

| or

& and

++ increment

-- decrement

~ not

- negation

Punctuation:
{} condition action

() function arguments

[] condition

, statement separator (display value)
; statement separator
: keyword/action separator
Event Broadcasts:
event - Broadcast event to its parent and complete the resulting executions.

state.event - Broadcast event to its parent state; evaluate the state if it is active.
send(state,event) - Direct event to state and evaluate state, if active.

Function Calls:
ml.name(arg1, arg2, )
name(arg1, arg2, )
name()
MATLAB function call
Custom code function call
Function call with empty argument list

Flow Diagrams within States

Flow diagram notation is essentially logic represented without the use of states. Flow diagram notation eliminates the use of unnecessary states and produces more efficient code with optimized memory usage. Flow diagram notation is represented through combinations of transitions to and from connective junctions.

This diagram shows flow diagram notation used within a state:.


Chart Execution

Event Processing

Stateflow processes one event at a time. When an event broadcast occurs, chart execution switches to the new event. The processing of an event involves two steps:

   1.
Searching for a valid transition path
   2.
Executing the transition path

Valid Transition Paths

When an event occurs, Stateflow uses semantic rules to interpret the chart logic and to construct the resulting transition path. A valid transition path is an implied path between an active origin state and an active destination state. The implied path can contain one or more of the following: transitions, connectives, default transitions, history junctions, and implied transitions from a child to its parent.

This figure shows the transition path from state S1.A to state S2.S3.C when event E occurs


:

Searching for a Transition Path

Stateflow uses a combination of hierarchy and ordering rules to determine the valid transition path. It is important to note that valid actions are executed during the path search.

The path search begins with the active state; all transitions that originate from the active state are tested. Ordering rules determine the sequence in which the transitions are tested. If a transition branches or loops through a connective, the outgoing transitions from the connective are tested. Searching is terminated when the first valid path is found.

Top-Down Searching

Searching starts at the outermost active state. The search steps include:

   1.
Test all outgoing transitions for a valid path.
   2.
Execute during action.
   3.
Execute on event_name action.
   4.
Test inner transitions for a valid path.
If these steps fail to produce a valid path, the next active substate in the hierarchy is activated and the search steps are repeated. As an example, consider the execution of this Stateflow chart:

After the default transition executes, state S3 becomes active. The path out of state S3 is dependent on which event is broadcast:

Event Broadcast
Result
E1
No during actions are executed.

E2
The during action f1() is executed.
E3
The during actions f2() and f3() are executed.

A substate can be active only when its parent state is active. As a result, substates are entered last and exited first. Regardless of which event is broadcast in the chart on page 5-14, the exit actions will be executed in the order: g1(), g2(), and g3().

Testing Order

Consider the group of transitions that originate from the connective in the diagram:


The numbers near the transition arrowheads indicate the sequence in which they are tested. This table provides the ordering rules that Stateflow uses:

Rule Priority
Description
Transitionfrom chart on page 5-15
1
Parent Rule: Transitions are sorted by the level of their parent. This means that outermost transitions are tested first.

1, 2
2
Transition Label Rule: Transitions with the same level of parent are sorted by their label. The labels are considered in the following order:
   1.
Labels that include events and conditions
   2.
Labels with events only
   3.
Labels with conditions only
   4.
Labels with no events or conditions

3, 4, 5, 6
3
Transition Origin Rule: Transitions with the same level parent and the same type of label are sorted by their origin point using object geometry. The rule applies to both state and connective origins:
5, 6

Path Execution

As a valid transition path is executed, states are marked active or inactive. The sequence of marking a state active or inactive is equivalent to the order in which state boundaries are crossed with the path. In addition, state entry actions, state exit actions, and transition actions are executed as follows:

This figure shows the diagram on page 5-13 with the addition of labels to mark critical points along the transition path:


Referring to these labels, consider the execution of the transition path from state S1.A to S2.S3.C:

   1.
The exit action of state S1.A executes and S1.A is marked inactive.
   2.
The exit action of state S1 executes and S1 is marked inactive.
   3.
All transition actions execute in the order in which they are encountered along the path.
   4.
State S2 is marked active and its entry action is executed.
   5.
State S2.S3 is marked active and its entry action is executed.
   6.
State S2.S3.C is marked active and its entry action is executed.

Frequently Asked Questions

This table contains frequently asked questions and their answers:

Questions Answers
What are the differences among the terms Stateflow diagram, chart, and Stateflow block? Using Stateflow, you create Stateflow diagrams. A Stateflow diagram is also a graphical representation of a finite state machine where states and transitions form the basic building blocks of the system. The term chart is used interchangeably with the term Stateflow diagram and is used as a shorthand representation in the GUIs.

The Stateflow block is a masked Simulink model and is equivalent to an empty, untitled Stateflow diagram. Use the Stateflow block to include a Stateflow diagram in a Simulink model.

The term Stateflow diagram is used from the Stateflow point of view, whereas the term Stateflow block is used from the Simulink point of view.

How do I load a Stateflow diagram? Stateflow diagrams exist only in Simulink models and are saved in the .mdl file along with the Simulink model information. When a Simulink model is loaded, any Stateflow diagrams contained within it are also loaded.
To load a Stateflow diagram, you simply load its associated Simulink model.
How do I connect two states with a transition? There are two ways to do this:
Focus your mouse over an edge of the first state (the cursor should become a cross-hair indicating that a transition will be created) and click. You then drag your mouse until you hit an edge of the second state at which point the transition should snap into place.
Alternatively, you can drag a default transition off the toolbar and snap it to the edge of a state. You will then need to connect the source of the default transition to the other state.
How do I copy data, events, and targets that I have created using the Explorer? There is currently no mechanism for copying or pasting objects in the Explorer. To create a copy you need to explicitly create a duplicate object.
How do I change the font size of a collection of objects. You can do this two ways:
   1.
Use the Font Size menu option on the graphics editor Style menu.
   2.
Use the graphics editor's context sensitive shortcut menu:
  1. Right click in an open area of the graphics editor drawing area. A pop-up menu appears.
  2. Click on the Font Size menu item and drag horizontally until the desired font size is displayed.
How do I change the decomposition of my states from exclusive (OR) to parallel (AND)?
To change the decomposition at a specified level in the hierarchy:

   1.
Select the parent state whose composition you want to change.
   2.
Display the shortcut menu by right clicking while the cursor is over the state.
   3.
Select Parallel (AND) from the shortcut menu.
How do I call Simulink from Stateflow and vice versa? You can define data and events to be either Input from Simulink or Output to Simulink. You can use these objects to exchange information with the Simulink model.
How do I use MATLAB functions and variables in my Stateflow diagrams? Stateflow provides two action language notations to use MATLAB functions and variables:
See the Stateflow User's Guide for detailed information on these action language notations.
I defined several Imported and Exported events and data in a Stateflow diagram. Why can't I use them in Simulink blocks that belong to the same model?
Data and events parented by the machine can have Exported and Imported scope. You might think they are somehow shared with Simulink objects. It is important to note that Imported/Exported events and data have nothing to do with normal simulation. They are provided for use with stand-alone code generation for advanced users.
If I change a data Workspace object from the MATLAB command line during a simulation, will Stateflow see the change?
If Stateflow changes a data Workspace object, will its value as accessed from the MATLAB command line change?
You need to define data Workspace objects in the MATLAB command window before starting the simulation. At the start of the simulation the value of the data Workspace object is evaluated and used as such by Stateflow. The Stateflow block may update the values of the data object; any changes can be observed at the MATLAB command line. However, if a data Workspace object is changed at the MATLAB command line during a simulation, it is ignored by Stateflow. That is, the Stateflow block continues to use the value as defined at the start of simulation or as changed by Stateflow during the simulation. After simulation starts, do not modify data Workspace objects from the MATLAB command line; this action will corrupt the link between the Stateflow block and the MATLAB workspace and any modified values computed by the Stateflow block will be lost.
What is the difference between the Parse and Parse Diagram menu items on the graphics editor Tools menu? The Parse menu item parses all the Stateflow diagrams for the current model/machine. Parse Diagram parses the current diagram only.
I am having trouble generating code for the simulation target. What could be wrong? Proper MEX setup is required for Stateflow to generate the S-function for simulation. Make sure that you have followed the recommended order of installation of MATLAB, Simulink, and Stateflow relative to the C compiler installation. (See the MATLAB Application Program Interface Guide for information about installing compilers.)
Some of the error messages related to code generation have what looks like ID numbers in them. What are those numbers? Occasionally you will see numbers preceded by a # character in error messages. This number is a unique ID (or handle) that represents an object in the data dictionary. These IDs are unique to a particular session (similar to Handle Graphics object handles). The line in the pop-up window that contains the ID is a hypertext link. Double-clicking on that error line in the pop-up window brings the graphics editor window to the front, zooms on the relevant object, and selects the ID of the object causing the problem.
When I start simulation on my Stateflow diagram, I see the following error message in a dialog box.
"Error using => toolbox/stateflow/stateflow/private/(intfcn_method). Stateflow S-Function build failed."
Why?
Error messages related to code generation can originate from the parser, the simulation code generator, Stateflow Coder, or from the external C compiler. If you are explicitly parsing, generating code, or building a target, it is apparent which operation generated the error.

It is less clear which operation generated the error if you are implicitly performing these operations through one of these methods:

Errors are displayed (in red) in an informational dialog box and also in the MATLAB command window. Note that whenever you see a line in the error message dialog box, you can double-click on that line to zoom in on the object in the Stateflow diagram.

See the Stateflow User's Guide for more information.
How do I animate my Stateflow diagram during simulation?
The sfun simulation target has debugging/animation enabled by default. However, if this setting has been changed, you may need to re-enable it. Invoke the Explorer and select the machine/model you want to simulate.
   1.
Choose Open Simulation Target from the Tools menu to display the sfun Target Configuration properties dialog box.
   2.
Enable animation and click on the Apply button.
   3.
Select Debug on the graphics editor Tools menu.
   4.
Ensure that animation is enabled in the Debugger window. Click on the Go button to run the simulation.
If you encounter errors, see the Stateflow User's Guide for more information.

How do I incorporate my own code into the Stateflow generated code? When you generate code for a Stateflow machine you use objects called targets. There are potentially several targets for any given machine. Stateflow provides a default simulation target called sfun. The option of creating multiple targets allows you to generate code in different ways. Essentially, target objects embody code generation configuration information.
Each target object has an associated Configuration properties dialog box that includes a Custom Code field. You can incorporate your own code into the Stateflow generated code by entering the code in this field. The text placed in the Custom Code field is inserted at the top of the Stateflow generated source code. You may want to include some global variable declarations or preprocessor directives such as #include or #define.
To include external custom libraries, use the --l MEX command option in the Configuration properties dialog box Make Command field.
See the Stateflow User's Guide for information on displaying the various target Configuration properties dialog boxes.
For Simulink parameters I have a fixed-step solver with a 1 second step size. I drive the Stateflow block with a 1Hz pulse going into a rising edge trigger. Why doesn't the chart ever execute?
Why doesn't it execute with a falling edge trigger either?
You've specified that the Stateflow block is to be triggered based on a 1Hz rising input event. The Simulink model solver and step size dictate how and when the Simulink model executes. When executed once per second, the 1Hz pulse looks like a constant to the rest of the system with no edges to trigger on. This combination of Simulink parameters and Stateflow block triggering does not allow the Stateflow block to execute. There is not enough time for the rising edge to be detected.
Changing to falling edge doesn't work either for the same reason. The solver must execute at a rate greater than twice that of the highest system frequency of concern. In this case, the maximum step size must be less than 0.5 second.
How can I use a single Output to Simulink event to execute several subsystems in a predetermined order? You can do this by cascading the subsystems through their triggers. Define the event as an Output to Simulink and include a trigger block within each of the subsystems. Connect the Stateflow event output directly to the trigger input of the first subsystem to be executed. Set up the subsystem trigger block to have Show Output Port selected and connect the output port as a subsystem output. Connect the subsystem output to the trigger input of the next subsystem to execute. Any number of subsystems can be chained together to control execution order.
To use this method, the Simulink subsystem triggers must be specified as function-call or either-edge.
Pressing the Esc key seems to cause different behavior in Stateflow than it does in Simulink. What are the differences? Pressing the Esc key in Simulink closes the current window and causes the parent of that window to come to the front. Repeatedly pressing the Esc key results in the same behavior until the root of the hierarchy is reached.
Pressing the Esc key in Stateflow cancels the last operation and deselects any selected objects. Pressing the Esc key a second time causes the parent of that window (the Simulink model window) to come to the front. Stateflow does not close the graphics editor window.
The primary difference is that Simulink closes lower level windows while proceeding up the hierarchy, while Stateflow does not.
Why can't I get a prompt in the MATLAB command window while I'm debugging? When the Debugger is in operation, it is considered the current active figure. This means that input from the MATLAB command window is disabled. The Debugger provides a MATLAB command field in its main window to allow access to the MATLAB command line.
Where does the graph go when I plot from the Debugger MATLAB command line field?
Executing Handle Graphics calls while using Stateflow can give unexpected results because Stateflow GUIs are based on Handle Graphics. When you issue a plot command from the MATLAB command field in the Debugger, the Stateflow graphics editor window is considered the current figure. To have the graph appear in a different figure from the current one (the graphics editor window), explicitly open a new figure (e.g., figure(2)).



[ Previous | Help Desk | Next ]