Main Content

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:

  • A value is assigned 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.

  • MATLAB assigns a default value to the property during initialization of an object before calling object constructor functions.

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

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

Setting Property Value in Constructor

Setting a property value in the constructor causes the property set method to be called. For example, the PropertySetMethod class defines a property set method for the Prop1 property.

classdef PropertySetMethod
   
   properties
      Prop1 = "Default String"
   end
   
   methods
      function obj = PropertySetMethod( str )
         if nargin > 0
            obj.Prop1 = str;
         end
      end
      
      function obj = set.Prop1(obj,str)
         obj.Prop1 = str;
         fprintf( 'set.Prop1 method called. Prop1 = %s\n', obj.Prop1 );
      end
   end
end

If you call the class constructor with no input arguments, MATLAB does not call the set.Prop1 method.

>> o = PropertySetMethod
o = 

  PropertySetMethod with properties:

    Prop1: "Default String"

Setting the property value in the constructor results in a call to the property set method.

>> o = PropertySetMethod("New string")
set.Prop1 method called. Prop1 = New string

o = 

  PropertySetMethod with properties:

    Prop1: "New string"

If you copy the object to another variable, MATLAB does not call the property set method even though the right side object in the assignment uses a nondefault value for the property:

a = o;
a.Prop1
a.Prop1

ans = 

    "New String"

Related Topics