Property Get and Set Methods - MATLAB & Simulink (original) (raw)
You can define property get and set methods that MATLABĀ® calls automatically whenever the associated property is accessed. To associate a get or set method with a given property, name the get and set methods using the forms get._`PropertyName`_
andset._`PropertyName`_
, respectively.
Get and set methods can perform extra steps beyond just accessing the property. Use get methods to:
- Calculate the value of dependent properties.
- Store data in a different format than what you present to users.
Use set methods to:
- Design property validation that is more complex than what the built-in validation techniques support.
- Issue custom error messages.
- Perform actions that are a direct result of a property value change, such as establishing or updating connections with hardware devices or opening files, ensuring access to resources.
Get and set methods do add overhead to your classes. Avoid complex and computation-heavy operations in the get and set methods of frequently accessed properties.
Note
You cannot call the get and set methods described in this topic directly. MATLAB automatically calls these methods when you access property values. For information on implementing user-callable get and set methods, see Implement Set/Get Interface for Properties.
Property Get Methods
You can define a get method that MATLAB automatically calls whenever the associated property value is queried. The get method must return the property value. Get methods use this syntax, where_PropertyName
_ is the name of the property.
methods function value = get.PropertyName(obj) ... end end
Method blocks defining get or set methods cannot specify attributes.
For example, the triangleArea
class defines a get method for theArea
property. Area
is defined as a dependent property, which means that it does not store values. The get method forArea
calculates the value on demand. (For more information on dependent properties, see Get and Set Methods for Dependent Properties.)
classdef triangleArea properties Base = 1 Height = 1 end properties (Dependent) Area end methods function a = get.Area(obj) disp("Executing get.Area method.") a = 0.5obj.Baseobj.Height; end end end
Create an instance of triangleArea
.
a =
Executing get.Area method. triangleArea with properties:
Base: 1
Height: 1
Area: 0.5000
When displaying an object, MATLAB calls any defined get methods for the properties it displays. In this case, it calls get.Area
and calculates the value ofArea
based on the default values for Base
and Height
. If a get method errors, MATLAB suppresses the error and omits that property from the display.
Change the values of Base
and Height
and access Area
again.
a.Base = 3; a.Height = 4; a.Area
Executing get.Area method.
ans =
6
Get Method Usage
- Get methods are not called recursively.
- When copying a value object (that is, not derived from the handle class), get methods are not called when copying property values from one object to another.
Property Set Methods
You can define a set method that MATLAB automatically calls whenever the associated property is assigned a value. Set methods use these syntaxes, depending on whether the class is a value or handle class:
- Value class set methods must return the modified object.
methods
function obj = set.PropertyName(obj,value)
...
end
end - Handle class set methods do not need to return the modified object.
methods
function set.PropertyName(obj,value)
...
end
end
Method blocks defining get or set methods cannot specify attributes.
For example, symPosDef
uses a set method for property validation. When the inputMatrix
property is set to a new value, the set method calls the chol function to determine if the input matrix is symmetric positive definite. If it is, the method setsinputMatrix
to that value. If not, the method returns a custom error message.
classdef symPosDef properties inputMatrix = [1 0; 0 1] end methods function obj = set.inputMatrix(obj,val) try chol(val) obj.inputMatrix = val; catch ME error("inputMatrix must be symmetric positive definite.") end end end end
Create an instance of symPosDef
and try to setinputMatrix
to a value that is not a symmetric positive definite matrix.
s = symPosDef; s.inputMatrix = [1 2; 1 1]
Error using symPosDef/set.inputMatrix inputMatrix must be symmetric positive definite.
Set Method Usage
- Set methods are not called recursively.
- MATLAB does not call set methods when it assigns default values to the properties during initialization of an object. However, setting property values in the constructor does call set methods.
- MATLAB calls set methods when an object is loaded.
- When MATLAB copies a value object (any object that is not a handle), set methods are not called when copying property values from one object to another.
- When a property is defined with the
AbortSet
attribute equal totrue
, the set method of the property is not called when assigning a value that is the same as the current value. However, if the property has a get method, that method is called so that the values can be compared. See Assignment When Property Value Is Unchanged for more information on this attribute.