Property Set Methods

Overview of Property Access Methods

For an overview of property access methods, see Property Access Methods

Property Set Method Syntax

MATLAB® calls a property's set method whenever a value is assigned to the property.

Note

You cannot call property access methods directly. MATLAB calls these methods when you access property values.

Property set methods have the following syntax, where PropertyName is the name of the property.

For a value class:

methods 
   function obj = set.PropertyName(obj,value) 
      ...
end
  • obj — Object whose property is being assigned a value

  • value — The new value that is assigned to the property

Value class set functions must return the modified object to the calling function. Handle classes do not need to return the modified object.

For a handle class:

methods 
   function set.PropertyName(obj,value) 
      ...
end

Use default method attributes for property set methods. The methods block defining the set method cannot specify attributes.

Validate Property Set Value

Use the property set method to validate the value being assigned to the property. The property set method can perform actions like error checking on the input value before taking whatever action is necessary to store the new property value.

classdef MyClass
   properties
      Prop1
   end
   methods
      function obj = set.Prop1(obj,value)
         if (value > 0)
            obj.Prop1 = value;
         else
            error('Property value must be positive')
         end
      end
   end
end

For an example of a property set method, see Restrict Properties to Specific Values .

When Set Method Is Called

If a property set method exists, MATLAB calls it whenever a value is assigned to that property. However, MATLAB does NOT call property set methods in the following cases:

  • Assigning a value to a property from within its own property set method, to prevent recursive calling of the set method. However, property assignments made from functions called by a set method do call the set method.

  • Initializing default values in class definitions when loading the class

  • Assigning a property to its default value that is specified in the class definition

  • Copying a value object (that is, not derived from the handle class). MATLAB does not call the set or get method when copying property values from one object to another.

  • Assigning a property value that is the same as the current value when the property’s AbortSet attribute is true. See Assignment When Property Value Unchanged for more information on this attribute.

As an optimization, if MATLAB determines that a property value does not change as a result of an assignment referencing the property, MATLAB does not call the property set method.

A set method for one property can assign values to other properties of the object. These assignments do call any set methods defined for the other properties. For example, a graphics window object can have a Units property and a Size property. Changing the Units property can also require a change to the values of the Size property to reflect the new units.

Was this topic helpful?