Property Access Methods

Property Setter and Getter Methods

Property access methods execute specific code whenever the associated property's value is queried or assigned a value. These methods enable you to perform a variety of 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® never calls the set method for a property of a particular class if the property value is set from within that set method. Similarly, the get method for a property of a particular class is never called if the property is queried from within that get method.

You can obtain the function handle for the set and get access methods from the property's object. The SetMethod and GetMethod properties contain the function handles for these methods.

Restrictions on Access Methods

You can 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 Implementing a Set/Get Interface for Properties for information on user-callable set and get methods.

Access Methods Cannot Call Other Functions to Access Property Values

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's 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's Methods property.

Access Method Function Handles

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

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

mc = ?ClassName;
mp = findobj(mc.PropertyList,'Name','PropertyName');
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.

Class Metadata provides more information on using meta-classes.

Function handles discusses the use of function handles.

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 Creating 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

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

See Assigning to Read-Only Properties Containing Objects for related information.

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;
Was this topic helpful?