hasChangedFrom

Detect change in data from specified value

Description

example

tf = hasChangedFrom(u,v) returns true if both of these conditions are true:

  • The value of u at the beginning of the previous time step was equal to v.

  • The value of u at the beginning of the current time step is not equal to v.

If multiple input events occur in the same time step, hasChangedFrom returns true when the value of u changes from the value v between input events.

The first argument 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:

  • 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. The hasChangedFrom operator returns true if the previous value of u was equal to v and any element of u has changed value since the last time step or input event.

    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 hasChangedFrom operator returns true if the previous value of u was equal to v and 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 the previous value of the structure struct was equal to structValue and any field of struct has changed value since the last time step or input event.

hasChangedFrom(struct,structValue)

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

hasChangedFrom(struct.field,5)

Returns true if the previous value of the matrix M was equal to matrixValue and any element of M has changed value since the last time step or input event.

hasChangedFrom(M,matrixValue)

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

In charts that use MATLAB® as the action language:

hasChangedFrom(M(1,3),7)

In charts that use C as the action language:

hasChangedFrom(M[0][2],7)

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 hasChangedFrom 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 hasChangedFrom operator. With this option enabled, the hasChangedFrom operator always returns false. For more information, see Initialize Outputs Every Time Chart Wakes Up.

Introduced in R2007a