Dependent properties do not store data. The value of a dependent property depends on some other value, such as the value of a nondependent property.
Dependent properties must define get-access methods (
get.) to determine a value for the property when the property is queried.
To be able to set the value of a dependent property, the property must define a set access method (
set.). The property set access method usually assigns the value to another, nondependent property for storage of the value.
For example, the
Account class returns a value for the dependent
Balance property that depends on the value of the
Currency property. The
get.Balance method queries the
Currency property before calculating a value for the
MATLAB® calls the
get.Balance method when the
Balance property is queried. You cannot call
Here is a partial listing of the class showing a dependent property and its get method:
classdef Account properties Currency DollarAmount end properties (Dependent) Balance end ... methods function value = get.Balance(obj) c = obj.Currency; switch c case 'E' v = obj.DollarAmount / 1.1; case 'P' v = obj.DollarAmount / 1.5; otherwise v = obj.DollarAmount; end format bank value = v; end end end
One application of a property get method is to determine the value of a property only when you need it, and avoid storing the value. To use this approach, set the property
Dependent attribute to
properties (Dependent = true) Prop end
The get method for the
Prop property determines the value of that property and assigns it to the object from within the method:
function value = get.
Prop(obj) value = calculateValue; ... end
get method calls a function or static method called
calculateValue to calculate the property value and returns
value as a result. The property get method can take whatever action is necessary within the method to produce the output value.
For an example of a property get method, see Calculate Data on Demand.
Although a dependent property does not store its value, you can define a set method for a dependent property to enable code to set the property.
propertyChange is a value class that changes the name of
a property from
can continue to allow the use of the old name without exposing it to new users. To
support the old property name, define
OldPropName as a dependent
property with set and get methods. As with nondependent properties, the set method in a
value class must return a modified object.
classdef propertyChange properties NewPropName end properties (Dependent, Hidden) OldPropName end methods function obj = set.OldPropName(obj,val) obj.NewPropName = val; end function value = get.OldPropName(obj) value = obj.NewPropName; end end end
There is no memory wasted by storing both old and new property values. Code that accesses
OldPropName continues to work as expected. Setting the
Hidden attribute of
OldPropName prevents new users from seeing the property.
Assignments made from property set methods cause the execution of any set methods defined for properties being set. See Calculate Data on Demand for an example.
If you use a dependent property only to return a value, then do not define a set access method for the dependent property. Instead, set the
SetAccess attribute of the dependent property to
private. For example, consider the following get method for the
methods function mval = get.MaxValue(obj) mval = max(obj.BigArray(:)); end end
This example uses the
MaxValue property to return a value that it calculates only when queried. For this application, define the
MaxValue property as dependent and private:
properties (Dependent, SetAccess = private) MaxValue end