Property Get Methods
Overview of Property Access Methods
For an overview of property access methods, see Property Access Methods.
Property Get Method Syntax
MATLAB® calls a property's get method whenever the property value is queried.
You cannot call property access methods directly. MATLAB calls these methods when you access property values.
Property get methods have the following syntax, where
PropertyName is the name of the property. The function must return the property value.
methods function value = get.PropertyName(obj) ... end
Calculate Value for Dependent Property
SquareArea class defines a dependent property
Area. MATLAB does not store a value for the dependent
Area property. When you query the value of the
Area property, MATLAB calls the
get.Area method calculates the value based on the
classdef SquareArea properties Width Height end properties (Dependent) Area end methods function a = get.Area(obj) a = obj.Width * obj.Height; end end end
Errors Not Returned from Get Method
The MATLAB default object display suppresses error messages returned from property get methods. MATLAB does not allow an error issued by a property get method to prevent the display of the entire object.
Use the property set method to validate the property value. Validating the value when setting a property ensures that the object is in a valid state. Use the property get method only to return the value that the set method has validated.
Get Method Behavior
MATLAB does NOT call property get methods in the following cases:
Getting a property value from within its own property get method, which prevents recursive calling of the get method
Copying a value object (that is, not derived from the
handleclass). The set or get method is not called when copying property values from one object to another.