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.

Assignment When Property Value Is Unchanged

AbortSet When Value Does Not Change

When you set a property value, MATLAB® triggers the property PreSet and PostSet events, invokes the property set method (if one is defined), and sets the property value. These actions occur even when the current value of the property is the same as the new value.

You can prevent these actions by setting the property's AbortSet attribute to true. When AbortSet is enabled, MATLAB compares the current property value to the new value being assigned to the property. If the new value is the same as the current value, MATLAB does not:

  • Set the property value.

  • Trigger the PreSet and PostSet events.

  • Call the property set method, if one exists.

To compare values, MATLAB must get the current value of the property. Getting the current value causes the property get method (get.Property) to execute, if one exists. Any errors that occur when calling the property get method are visible to the user, even if MATLAB does not change the current value.

How MATLAB Compares Values

MATLAB uses the isequal function to determine if the current value of the property is the same as the new value. To determine if specific values evaluate as equal when using the AbortSet attribute, see the isequal function documentation or any isequal method overloaded for the class of the property value.

When to Use AbortSet

Use of the AbortSet attribute does incur some overhead in the comparison of the current and new property values. Using the AbortSet attribute can slow all property assignments because the current and assigned value are always compared before the assignment is made. The AbortSet attribute is most useful when:

  • You want to prevent notification of the PreSet and PostSet events and execution of the listener callbacks when the property value does not change.

  • The cost of setting a property value is greater than the cost of comparing the current property value with the value being assigned, and you are willing to incur the comparison cost with all assignments to the property.

Implement AbortSet

The following example shows how the AbortSet attribute works. The AbortTheSet class defines a property, PropOne, that has listeners for the PreGet, PreSet, PostGet, and PostSet events and enables the AbortSet attribute.

Note

To use this class, save the AbortTheSet class in a file with the same name in a folder on your MATLAB path.

classdef AbortTheSet < handle
    properties (SetObservable, GetObservable, AbortSet)
        PropOne = 7
    end
    methods
        function obj = AbortTheSet
            addlistener(obj,'PropOne','PreGet',@obj.getPrePropEvt);
            addlistener(obj,'PropOne','PreSet',@obj.setPrePropEvt);
            addlistener(obj,'PropOne','PostGet',@obj.getPostPropEvt);
            addlistener(obj,'PropOne','PostSet',@obj.setPostPropEvt);
        end
        function propval = get.PropOne(obj)
            disp('get.PropOne called')
            propval = obj.PropOne;
        end
        function set.PropOne(obj,val)
            disp('set.PropOne called')
            obj.PropOne = val;
        end
        function getPrePropEvt(obj,src,evnt)
            disp ('Pre-get event triggered')
            % ...
        end
        function setPrePropEvt(obj,src,evnt)
            disp ('Pre-set event triggered')
            % ...
        end
        function getPostPropEvt(obj,src,evnt)
            disp ('Post-get event triggered')
            % ...
        end
        function setPostPropEvt(obj,src,evnt)
            disp ('Post-set event triggered')
            % ...
        end
        function disp(obj)
            % Overload disp to avoid accessing property
            disp (class(obj))
        end
    end
end

The class specifies an initial value of 7 for the PropOne property. Therefore, if you create an object and assign the property value of 7, there is no need to trigger the PreSet event. However, the getPropOne method is called to get the current value of the property to compare to the assigned vale.

obj = AbortTheSet;
obj.PropOne = 7;
get.PropOne called

If you specify a value other than 7, then MATLAB performs these steps:

  • Gets the current property value

  • Triggers the PreSet event

  • Sets the property to the assigned value

  • Triggers the PostSet event

obj = AbortTheSet;
obj.PropOne = 9;
get.PropOne called
Pre-set event triggered
set.PropOne called
Post-set event triggered

If you query the property value, the PreGet and PostGet events are triggered.

obj.PropOne
Pre-get event triggered
get.PropOne called
Post-get event triggered

ans =

     9

Using AbortSet with Property Validation

When classes use property validation and AbortSet in a property definition, MATLAB evaluates the property validation before comparing the current value to the value being assigned. For example, revise the AbortTheSet class to add a size restriction of 1-by-3 to the PropOne property.

classdef AbortTheSet < handle
    properties (SetObservable, GetObservable, AbortSet)
        % Restrict size to 1-by-3
        % ***********************
        PropOne (1,3) = [7 7 7]
        % ***********************
    end
    methods
        function obj = AbortTheSet
            addlistener(obj,'PropOne','PreGet',@obj.getPrePropEvt);
            addlistener(obj,'PropOne','PreSet',@obj.setPrePropEvt);
            addlistener(obj,'PropOne','PostGet',@obj.getPostPropEvt);
            addlistener(obj,'PropOne','PostSet',@obj.setPostPropEvt);
        end
        function propval = get.PropOne(obj)
            disp('get.PropOne called')
            propval = obj.PropOne;
        end
        function set.PropOne(obj,val)
            disp('set.PropOne called')
            obj.PropOne = val;
        end
        function getPrePropEvt(obj,src,evnt)
            disp ('Pre-get event triggered')
            % ...
        end
        function setPrePropEvt(obj,src,evnt)
            disp ('Pre-set event triggered')
            % ...
        end
        function getPostPropEvt(obj,src,evnt)
            disp ('Post-get event triggered')
            % ...
        end
        function setPostPropEvt(obj,src,evnt)
            disp ('Post-set event triggered')
            % ...
        end
        function disp(obj)
            % Overload disp to avoid accessing property
            disp (class(obj))
        end
    end
end

Because MATLAB applies scalar expansion to satisfy the size restriction, the following assignment does not trigger the PreSet or PostSet events.

obj = AbortTheSet;
obj.PropOne = 7;
get.PropOne called
obj.PropOne
Pre-get event triggered
get.PropOne called
Post-get event triggered

ans =

     7     7     7

For information on property validation, see Validate Property Values.

Related Topics