| Stateflow® | ![]() |
| On this page… |
|---|
A Stateflow® chart can detect changes in the following types of chart data from one time step to the next:
Inputs
Outputs
Local variables
Data bound to Simulink® data store memory
(For more information, see Sharing Global Data with Simulink® Models.)
For each of these types of data, you can use operators that detect the following changes:
| Type of Change | Operator |
|---|---|
Data changes value from the beginning of the last time step to the beginning of the current time step. | See hasChanged Operator. |
Data changes from a specified value at the beginning of the last time step to a different value at the beginning of the current time step. | |
Data changes to a specified value at the beginning of the current time step from a different value at the beginning of the last time step. |
Change detection operators return 1 if the data value changes or 0 if there is no change. See Change Detection Operators.
Stateflow software ships with a model sf_tetris2 that demonstrates how you can detect asynchronous changes in inputs — in this case, user keystrokes — to manipulate a Tetris shape as it moves through the playing field. The Stateflow chart TetrisLogic implements this logic:

TetrisLogic contains a subchart called Moving that calls the operator hasChanged to determine when users press any of the Tetris control keys, and then moves the shape accordingly. Here is a look inside the subchart:

To run the demo model from the MATLAB® workspace, follow these steps:
At the MATLAB command prompt, type:
demos
The MATLAB Help Browser opens the Demos tab in the Help Navigator pane.
In the Help Navigator pane, navigate to Simulink > Stateflow > General Applications.
In the right contents pane, click Tetris.
A description of the Tetris demo model appears.
In the upper right corner of the contents pane, click the link Open this model.
The model opens on your desktop.
A Stateflow chart detects changes in chart data by evaluating values at time step boundaries. That is, the chart compares the value at the beginning of the previous execution step with the value at the beginning of the current execution step. To detect changes, the chart automatically double-buffers these values in local variables, as follows:
| Local Buffer: | Stores: |
|---|---|
var_name_prev | Value of data at the beginning of the last time step |
var_name_start | Value of data at the beginning of the current time step |
Note Double-buffering occurs once per time step except if multiple input events occur in the same time step. Then, double-buffering occurs once per input event (see Handling Changes When Multiple Input Events Occur). |
When you invoke change detection operations in an action, Stateflow software performs the following operations:
Double-buffers data values after a Simulink event triggers the chart, but before the chart begins execution.
Compares values in _prev and _start buffers. If the values match, the change detection operator returns 0 (no change); otherwise, it returns 1 (change).
The following diagram places these tasks in the context of the chart life cycle:

The fact that buffering occurs before chart execution has implications for change detection in the following scenarios:
Stateflow software attempts to filter out transient changes in local chart variables by evaluating their values only at time boundaries (see How Change Detection Works). This behavior means that the software evaluates the specified local variable only once at the end of the execution step and, therefore, returns a consistent result. That is, the return value remains constant even if the value of the local variable fluctuates within a given time step.
For example, suppose that in the current time step a local variable temp changes from its value at the previous time step, but then reverts to the original value. In this case, the operator hasChanged(temp) returns 0 for the next time step, indicating that no change occurred. For more information, see Change Detection Operators.
When multiple input events occur in the same time step, Stateflow software updates the _prev and _start buffers once per event. In this way, a chart detects changes between input events, even if the changes occur more than once in a given time step.
Change detection operators check for changes in chart inputs, outputs, and local variables, and in Stateflow data that is bound to Simulink data store memory.
You can invoke change detection operators wherever you call built-in functions in a chart — in state actions, transition actions, condition actions, and conditions. There are three change detection operators:
| hasChanged Operator | Detects any change since the last time step |
| hasChangedFrom Operator | Detects whether data changes from a specified value |
| hasChangedTo Operator | Detects whether data changes to a specified value |
The hasChanged operator detects any change in Stateflow data since the last time step, using the following heuristic:
![]()
where xstart represents the value at the beginning of the current time step and xprev represents the value at the beginning of the previous time step.
hasChanged ( u ) hasChanged ( m [ expr ] ) hasChanged ( s [ expr ] )
where u is a scalar or matrix variable, m is a matrix, and s is aggregate data.
The arguments u, m, and s must be one of the following data types:
Input, output, or local variable in a Stateflow chart
Stateflow data that is bound to Simulink data store memory
The arguments cannot be expressions or custom code variables.
Description. hasChanged ( u ) returns 1 if u changes value since the last time step. If u is a matrix, hasChanged returns 1 if any element of u changes value since the last time step.
hasChanged ( m [ expr ] ) returns 1 if the value at location expr of matrix m changes value since the last time step. expr can be an arbitrary expression that evaluates to a scalar value.
hasChanged ( s [ expr ] ) returns 1 if the value at location expr of aggregate data s has changed since the last time step. s must be a fully qualified name, such as u.foo.bar, which resolves to an aggregate data type such as a structure or bus signal. expr can be an arbitrary expression that evaluates to a scalar value.
All forms of hasChanged return zero if a chart writes to the data, but does not change its value.
The hasChangedFrom operator detects when Stateflow data changes from a specified value since the last time step, using the following heuristic:
![]()
where xstart represents the value at the beginning of the current time step and xprev represents the value at the beginning of the previous time step.
hasChangedFrom ( u , v ) hasChangedFrom ( m [ expr ], v ) hasChangedFrom ( s [ expr ], v )
where u is a scalar or matrix variable, m is a matrix, and s is aggregate data.
The arguments u, m, and s must be one of the following data types:
Input, output, or local variable in a Stateflow chart
Stateflow data that is bound to Simulink data store memory
Note The first arguments u, m, and s cannot be expressions or custom code variables. The second argument v can be an expression. However, if the first argument is a matrix variable, then v must resolve to a scalar value or a matrix value with the same dimensions as the first argument. |
Description. hasChangedFrom ( u, v ) returns 1 if u changes from the value specified by v since the last time step. If u is a matrix variable whose elements all equal the value specified by v, hasChangedFrom returns 1 if one or more elements of the matrix changes to a different value in the current time step.
hasChangedFrom ( m [ expr ], v) returns 1 if the value at location expr of matrix m changes from the value specified by v since the last time step. expr can be an arbitrary expression that evaluates to a scalar value.
hasChangedFrom ( s [ expr ], v) returns 1 if the value at location expr of aggregate data s changes from the value specified by v since the last time step. s must be a fully qualified name, such as u.foo.bar, which resolves to an aggregate data type such as a structure or bus signal. expr can be an arbitrary expression that evaluates to a scalar value.
The hasChangedTo operator detects when Stateflow data changes to a specified value since the last time step, using the following heuristic:
![]()
where xstart represents the value at the beginning of the current time step and xprev represents the value at the beginning of the previous time step.
hasChangedTo ( u , v ) hasChangedTo ( m [ expr ], v ) hasChangedTo ( s [ expr ], v )
where u is a scalar or matrix variable, m is a matrix, and s is aggregate data.
The arguments u, m, and s must be one of the following data types:
Input, output, or local variable in a Stateflow chart
Stateflow data that is bound to Simulink data store memory
Note The first arguments u, m, and s cannot be expressions or custom code variables. The second argument v can be an expression. However, if the first argument is a matrix variable, then v must resolve to either a scalar value or a matrix value with the same dimensions as the first argument. |
Description. hasChangedTo ( u, v ) returns 1 if u changes to the value specified by v in the current time step. If u is a matrix variable, hasChangedTo returns 1 if any its of its elements changes value so that all elements of the matrix equal the value specified by v in the current time step.
hasChangedTo ( m [ expr ], v) returns 1 if the value at location expr of matrix m changes to the value specified by v in the current time step. expr can be an arbitrary expression that evaluates to a scalar value.
hasChangedTo ( s [ expr ], v) returns 1 if the value at location expr of aggregate data s changes to the value specified by v in the current time step. s must be a fully qualified name, such as u.foo.bar, which resolves to an aggregate data type such as a structure or bus signal. expr can be an arbitrary expression that evaluates to a scalar value.
The following model shows how to use the hasChanged, hasChangedFrom, and hasChangedTo operators to detect specific changes in an input signal. In this example, a Ramp block sends a discrete, increasing time signal to a Stateflow chart, as follows:

The model uses a fixed-step solver with a step size of 1. The signal increments by 1 at each time step. The chart analyzes the input signal for the following changes at each time step:
Any change from the previous time step
Change to the value 3
Change from the value 3
To check the signal, the chart calls three change detection operators in a transition action, and outputs the return values as y1, y2, and y3, as follows:

During simulation, the outputs y1, y2, and y3 represent changes in the input signal, as shown in this scope:

![]() | Using Temporal Logic in State Actions and Transitions | Checking State Activity | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |