This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

hasChanged

Detect change in data since last time step

Syntax

tf = hasChanged(u)

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 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.

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

  • 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.

  • The hasChanged operator is supported only in Stateflow® charts in Simulink® models.

Introduced in R2007a