Detect Changes in Data Values

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.

Change Detection Operators

To detect changes in Stateflow data, use the operators listed in this table.

OperatorSyntaxDescriptionExample
hasChangedtf = hasChanged(u)

Detects changes in data value from the beginning of the last time step to the beginning of the current time step.

Returns true if u has changed value.

hasChanged(u)
hasChangedFromtf = hasChangedFrom(u,v)

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.

Returns true if u has changed from the value v.

hasChangedFrom(u,v)
hasChangedTotf = hasChangedTo(u,v)

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.

Returns true if u has changed to the value v.

hasChangedTo(u,v)

Change detection operators return true if there is a change in data value and false if there is no change in data value.

The argument u can be:

Indices can be numbers or expressions that evaluate to a scalar value.

For the hasChangedFrom and hasChangedTo operators, the second argument v can be any expression that resolves to a value that is comparable with u.

  • If u is a scalar, then v must resolve to a scalar value.

  • If u is a matrix, then v must resolve to a matrix value with the same dimensions as u.

    Alternatively, in a chart that uses C as the action language, v 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 by v. See Assign Values to All Elements of a Matrix.

  • If u is a structure, then v must resolve to a structure value whose field specification matches u exactly.

The scope of u depends on the chart action language:

  • MATLAB® as the action language: Input only.

  • C as the action language: Input, Output, Local, or Data Store Memory.

The argument u cannot be a nontrivial expression or a custom code variable.

Example of Chart with Change Detection

This model shows how the operators hasChanged, hasChangedFrom, and 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 y1, y2, and y3.

During simulation, the Scope block shows the input and output signals for 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.

Implementation of Change Detection

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.

Local BufferDescription

var_name_prev

Value of data var_name at the beginning of the last time step

var_name_start

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 _prev and _start buffers. If the values match, the change detection operator returns false (no change); otherwise, it returns true (change).

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.

Filter Out Transient Value Changes

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 hasChanged(temp) returns false for the next time step, indicating that no change occurred.

Detect Value Changes Between Input Events

When multiple input events occur in the same time step, the Stateflow chart updates the _prev and _start buffers once per event. A chart detects changes between input events, even if the changes occur more than once in a given time step.

See Also

| |

Related Topics