hasChangedTo
Detect change in data to specified value
Description
tf = hasChangedTo(
returns 1 (data_name
,value
)true
) if the value of data_name
was
not equal to the specified value
at the beginning of the previous
time step and is equal to value
at the beginning of the current
time step. Otherwise, the operator returns 0 (false
).
The argument data_name
can be:
A scalar variable.
A matrix or an element of a matrix.
If
data_name
is a matrix, the operator returnstrue
when it detects a change in any element ofdata_name
and the new value ofdata_name
is equal tovalue
.Index elements of a matrix by using numbers or expressions that evaluate to a constant integer. See Supported Operations for Vectors and Matrices.
A structure or a field in a structure.
If
data_name
is a structure, the change detection operator returnstrue
when it detects a change in any element ofdata_name
and the new value ofdata_name
is equal tovalue
.Index fields in a structure by using dot notation. See Index and Assign Values to Stateflow Structures.
Any valid combination of structure fields or matrix elements.
The argument data_name
cannot be a nontrivial expression or a
custom code variable.
Note
Standalone charts in MATLAB® do not support change detection on an element of a matrix or a field in a structure.
The argument value
can be any expression that resolves to a
value that is comparable with data_name
:
If
data_name
is a scalar, thenvalue
must resolve to a scalar value.If
data_name
is a matrix, thenvalue
must resolve to a matrix value with the same dimensions asdata_name
.Alternatively, in a chart that uses C as the action language,
value
can resolve to a scalar value. The chart uses scalar expansion to comparedata_name
to a matrix whose elements are all equal to the value specified byvalue
. See Assign Values to All Elements of a Matrix.If
data_name
is a structure, thenvalue
must resolve to a structure value whose field specification matchesdata_name
exactly.
Examples
Tips
If multiple input events occur in the same time step, the
hasChangedTo
operator can detect changes in data value between input events.If the chart writes to the data object but does not change the data value, the
hasChangedTo
operator returnsfalse
.The type of Stateflow® chart determines the scope of the data supported by the change detection operators:
Standalone Stateflow charts in MATLAB:
Local
onlyIn Simulink® models, charts that use MATLAB as the action language:
Input
onlyIn Simulink models, charts that use C as the action language:
Input
,Output
,Local
, orData Store Memory
In a standalone chart in MATLAB, a change detection operator can detect changes in data specified in a call to the
step
function because these changes occur before the start of the current time step. For example, ifx
is equal to zero, the expressionhasChangedTo(x,1)
returnstrue
when you execute the chartch
with the command:In contrast, a change detection operator cannot detect changes in data caused by assignments in state or transition actions in the same time step. Instead, the operator detects the change in value at the start of the next time step.step(ch,x=1);
In a chart in a Simulink model, if you enable the chart option Initialize Outputs Every Time Chart Wakes Up, using an output as the argument of the
hasChanged
operator always returnsfalse
. For more information, see Initialize outputs every time chart wakes up.When row-major array layout is enabled in charts that use
hasChangedTo
, code generation produces an error. Before generating code in charts that usehasChangedTo
, enable column-major array layout. See Select Array Layout for Matrices in Generated Code.