hasChangedTo

Detect change in data to specified value

Description

example

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

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

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

If multiple input events occur in the same time step, hasChangedTo returns true when the value of u changes to 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 hasChangedTo operator returns true if any element of u has changed value since the last time step or input event and the current value of u is equal to v.

    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 hasChangedTo operator returns true if any field of u has changed value since the last time step or input event and the current value of u is equal to v.

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

Examples

expand all

Returns true if any field of struct has changed value since the last time step or input event and the current value of the structure struct is equal to structValue.

hasChangedTo(struct,structValue)

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

hasChangedTo(struct.field,5)

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

hasChangedTo(M,matrixValue)

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

In charts that use MATLAB® as the action language:

hasChangedTo(M(1,3),7)

In charts that use C as the action language:

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

Introduced in R2007a