Property Access Methods

Property Setter and Getter Methods

Property access methods execute specific code whenever the property value is queried or assigned a value. These methods enable you to perform various operations:

  • Execute code before assigning property values to perform actions such as:

  • Execute code before returning the current values of properties to perform actions such as:

Calling Access Methods

    Note:   You cannot call property access methods directly.

Property access methods execute automatically whenever you set or query the corresponding property values from outside the access method. MATLAB® does not call access methods recursively. That is, MATLAB does not call the set method when setting the property from within its set method. Similarly, MATLAB does not call the get method when querying the property value from within its get method.

Obtain the function handle for the set and get access methods from the property object. The SetMethod and GetMethod properties contain the function handles that refer to these methods.

Restrictions on Access Methods

Define property access methods only:

  • For concrete properties (that is, properties that are not abstract)

  • Within the class that defines the property (unless the property is abstract in that class, in which case the concrete subclass must define the access method).

MATLAB has no default set or get property access methods. Therefore, if you do not define property access methods, MATLAB software does not invoke any methods before assigning or returning property values.

Once defined, only the set and get methods can set and query the actual property values. See Set Method Behavior for information on cases where MATLAB does not call property set methods.

    Note:   Property set and get access methods are not equivalent to user-callable set and get methods used to set and query property values from an instance of the class. See Implement Set/Get Interface for Properties for information on user-callable set and get methods.

Access Methods Cannot Call Functions to Access Properties

You can set and get property values only from within your property set or get access method. You cannot call another function from the set or get method and attempt to access the property value from that function.

For example, an anonymous function that calls another function to do the actual work cannot access the property value. Similarly, an access function cannot call another function to access the property value.

Defining Access Methods

Access methods have special names that include the property name. Therefore, get.PropertyName executes whenever PropertyName is referenced and set.PropertyName executes whenever PropertyName is assigned a value.

Define property access methods in a methods block that specifies no attributes. You cannot call these methods directly. MATLAB calls these methods when any code accesses the properties.

Property access methods do not appear in the list of class methods returned by the methods command and are not included in the meta.class object Methods property.

Access Method Function Handles

The property object contains function handles to the property set and get methods. SetMethod contains a function handle to the set method. GetMethod contains a function handle to the get method.

Obtain these handles from the object:

mc = ?ClassName;
mp = findobj(mc.PropertyList,'Name','PropertyName');
fh = mp.GetMethod;

For example, if the class MyClass defines a get method for its Text property, you can obtain a function handle to this function from the meta.class object:

mc = ?MyClass;
mp = findobj(mc.PropertyList,'Name','Text');
fh = mp.GetMethod;

The returned value, fh, contains a function handle to the get method defined for the specified property name for the specified class.

For information on defining function handles, see Create Function Handle

Set and Get Method Execution and Property Events

MATLAB software generates events before and after set and get operations. You can use these events to inform listeners that property values have been referenced or assigned. The timing of event generation is as follows:

  • PreGet — Triggered before calling the property get method

  • PostGet — Triggered after the property get method has returned its value

If a class computes a property value (Dependent = true), then the behaviors of its set events are like the get events:

  • PreSet — Triggered before calling the property set method

  • PostSet — Triggered after calling the property set method

If a property is not computed (Dependent = false, the default), then the assignment statement with the set method generates the events:

  • PreSet — Triggered before assigning the new property value within the set method

  • PostSet — Triggered after assigning the new property value within the set method

For information about using property events, see Create Property Listeners.

Access Methods and Properties Containing Arrays

You can use array indexing with properties that contain arrays without interfering with property set and get methods.

For indexed reference:

val = obj.PropName(n);

MATLAB calls the get method to get the referenced value.

For indexed assignment:

obj.PropName(n) = val;


  • Invokes the get method to get the property value

  • Performs the indexed assignment on the returned property

  • Passes the new property value to the set method

Access Methods and Arrays of Objects

When reference or assignment occurs on an object array, MATLAB calls the set and get methods in a loop. In this loop, MATLAB always passes scalar objects to set and get methods.

Modify Property Values with Access Methods

Property access methods are useful in cases where you want to perform some additional steps before assigning or returning a property value. For example, the Testpoint class uses a property set method to check the range of a value. It then applies scaling if it is within a particular range, and set it to NaN if it is not.

The property get methods applies a scale factor before returning its current value:

classdef Testpoint
      expectedResult = [];
      scalingFactor = 0.001;
      function obj = set.expectedResult(obj,erIn)
         if erIn >= 0 && erIn <= 100
            erIn = erIn.*obj.scalingFactor;
            obj.expectedResult = erIn;
            obj.expectedResult = NaN;
      function er = get.expectedResult(obj)
         er = obj.expectedResult/obj.scalingFactor;

More About

Was this topic helpful?