hasChanged

Detect change in data since last time step

Description

example

tf = hasChanged(u) returns true if the value of u at the beginning of the current time step is different from the value of u at the beginning of the previous time step. If multiple input events occur in the same time step, hasChanged returns true when the value of u changes between input events.

The argument u can be:

Indices can be numbers or expressions that evaluate to a scalar value. If u is a matrix, hasChanged returns true if any element of u has changed value since the last time step or input event. If u is a structure, hasChanged returns true if any field of u has changed value since the last time step or input event.

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

Examples

expand all

Returns true if any field of the structure struct has changed value since the last time step or input event.

hasChanged(struct)

Returns true if the structure field of struct.field has changed value since the last time step or input event.

hasChanged(struct.field)

Returns true if any element of the matrix M has changed value since the last time step or input event.

hasChanged(M)

Returns true if the element in row 1 and column 3 of the matrix M has changed value since the last time step or input event.

In charts that use MATLAB® as the action language:

hasChanged(M(1,3))

In charts that use C as the action language:

hasChanged(M[0][2])

Tips

  • For Stateflow® charts in a Simulink® model, the action language determines the scope of data that supports change detection:

    • MATLAB as the action language: Input only.

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

  • Standalone Stateflow charts do not support change detection on an element of a matrix or a field in a structure.

  • The hasChanged operator returns false if the chart writes to the data but does not change the data value.

  • If you enable the chart option Initialize Outputs Every Time Chart Wakes Up, do not use an output as the argument of the hasChanged operator. With this option enabled, the hasChanged operator always returns false. For more information, see Initialize Outputs Every Time Chart Wakes Up.

Introduced in R2007a