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:

Use set methods to:

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

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:

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

See Also

Topics