Stateflow® charts can detect changes in data values from one time step to the next time step. All Stateflow charts can detect changes in chart input data. Charts that use C as the action language can also detect changes in chart output data, local chart variables, and data store memory data.
Change detection is supported only in Stateflow charts in Simulink® models.
To detect changes in Stateflow data, use the operators listed in this table.
Detects changes in data value from the beginning of the last time step to the beginning of the current time step.
Detects changes in data value from a specified value at the beginning of the last time step to a different value at the beginning of the current time step.
Detects changes in data value 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
true if there is a change in data
false if there is no change in data value.
u can be:
Indices can be numbers or expressions that evaluate to a scalar value.
the second argument
v can be any expression that resolves to a value that
is comparable with
u is a scalar, then
v must resolve to a
u is a matrix, then
v must resolve to a
matrix value with the same dimensions as
Alternatively, in a chart that uses C as the action language,
can resolve to a scalar value. The chart uses scalar expansion to compare
u to a matrix whose elements are all equal to the value specified
v. See Convert Scalars to Nonscalars by Using Scalar Expansion.
u is a structure, then
v must resolve to
a structure value whose field specification matches
The scope of
u depends on the chart action language:
MATLAB® as the action language:
C as the action language:
Data Store Memory.
u cannot be a nontrivial expression or a custom code
This model shows how the operators
hasChangedTo detect specific changes in an input signal. In this example, a Ramp block sends a discrete, increasing time signal to a chart.
The model uses a fixed-step solver with a step size of 1. The signal increments by 1 at each time step. At each time step, the chart analyzes the input signal
u for these changes:
Any change from the previous time step
A change to the value 3
A change from the value 3
To check the signal, the chart calls three change detection operators in a transition action. The chart outputs the return values as
During simulation, the Scope block shows the input and output signals to the chart.
u increases by 1 at every time step.
y1 transitions to a value of 1 at time t = 1.
y1 remains 1 because
u continues to change at each subsequent time step.
y2 transitions to 1 at time t = 4 when the value of
u changes from 3 to 4.
y2 transitions back to 0 at time t = 5 when
u increases from 4 to 5.
y3 transitions to 1 at time t = 3 when the value of
u changes from 2 to 3.
y3 transitions back to 0 at time t = 4 when
u increases from 3 to 4.
A chart detects changes in chart data by evaluating values at time step boundaries. 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 double-buffers these values in local variables.
Value of data var_name at the beginning of the last time step
Value of data var_name at the beginning of the current time step
Double-buffering occurs once per time step except when multiple input events occur in the same time step. If multiple input events occur in the same time step, double-buffering occurs once per input event. See Detect Value Changes Between Input Events.
When you invoke a change detection operator in an action, the Stateflow chart:
Double-buffers data values after a Simulink event triggers the chart but before the chart begins execution.
Compares values in
If the values match, the change detection operator returns
(no change); otherwise, it returns
This diagram places these tasks in the context of the chart life cycle.
Buffering occurs before chart execution and affects change detection when:
Local variables have transient changes in value.
Multiple input events occur in the same time step.
Stateflow charts attempt to filter out transient changes in local chart variables by evaluating their values only at time boundaries. The chart evaluates the specified local variable only once at the end of the execution step. The return value of the change detection operators 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. The operator
false for the next time step, indicating that no change
When multiple input events occur in the same time step, the Stateflow chart updates the
buffers once per event. A chart detects changes between input events, even if the changes
occur more than once in a given time step.