Property Access Methods

Properties Provide Access to Class Data

In MATLAB®, properties can have public access. Therefore, properties can provide access to data that the class design exposes to users.

Use property access methods to provide error checking or to implement side effects resulting from property access. Examples of access methods include functions that update other property values when setting the property or translate the format of a property value before returning the value.

For specific information on access method syntax, see Property Get Methods and Property Set Methods.

You can use property validation to restrict the size, class, and other aspects of property values. For information on property validation, see Validate Property Values.

Performance Considerations with Access Methods

Property access methods do add the overhead of a function call whenever accessing property values. If performance-critical access to properties occurs inside methods of the class, define private properties to store values. Use these values inside methods without any error checking. For less frequent access from outside the class, define public Dependent properties that use access methods for error checking.

For information on access methods used with Dependent properties, see Set and Get Methods for Dependent Properties.

Property Set and Get 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:

To control what code can access properties, see Property Attributes.

When MATLAB Calls Access Methods

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 a property from within that property’s set method, no matter what instance of the class is being modified. Similarly, MATLAB does not call the get method when querying the property value from within that property’s own get method.

Note

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

Obtain the function handle for the set and get access methods from the property meta.property object. The meta.property 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 When Set Method Is Called 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 meta.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 meta.property 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;

MATLAB:

  • 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
   properties
      expectedResult = []
   end
   properties(Constant)
      scalingFactor = 0.001
   end
   methods
      function obj = set.expectedResult(obj,erIn)
         if erIn >= 0 && erIn <= 100
            erIn = erIn.*obj.scalingFactor;
            obj.expectedResult = erIn;
         else
            obj.expectedResult = NaN;
         end
      end
      function er = get.expectedResult(obj)
         er = obj.expectedResult/obj.scalingFactor;
      end
   end
end

Related Topics