Get and Set Methods for Dependent Properties - MATLAB & Simulink (original) (raw)

Main Content

Dependent properties do not store data. The value of a dependent property depends on other values, such as the values of nondependent properties. Define a dependent property using this syntax:

properties (Dependent) PropertyName end

Because dependent properties do not store data, you must define get methods (get._`PropertyName`_) to determine the value for the properties when the properties are queried.

Dependent properties can also have set methods (set._`PropertyName`_), but these methods cannot actually set the value of the dependent property. However, a set method can contain other code. For example, it can set values of properties related to the dependent property.

For an introduction to defining get and set methods, see Property Get and Set Methods.

Define a Get Method for a Dependent Property

The Account class stores an amount in US dollars, and it can return that value in one of three currencies: US dollars, euros, or Japanese yen. That converted value is represented by the dependent Balance property. Theget.Balance method uses USDollarAmount andCurrency to determine a conversion rate to calculate theBalance property.

classdef Account properties Currency {mustBeMember(Currency,["USD","EUR","JPY"])} = "USD" USDollarAmount = 0 end properties (Dependent) Balance end methods function value = get.Balance(obj) c = obj.Currency; switch c case "EUR" v = obj.USDollarAmount/0.98; case "JPY" v = obj.USDollarAmount/0.0069; otherwise v = obj.USDollarAmount; end value = v; end end end

Create an instance of Account. Set theUSDollarAmount and Currency properties.

a = Account; a.USDollarAmount = 100; a.Currency = "JPY";

You cannot explicitly call get methods. When you access Balance, MATLABĀ® calls the get method to return the initial amount converted to yen.

MATLAB also calls get methods when it displays the object. When you setCurrency to euros without ending the statement with a semicolon, MATLAB calls the Balance get method to display the updated value.

a =

Account with properties:

      Currency: "EUR"
USDollarAmount: 100
       Balance: 102.0400

When to Use Set Methods with Dependent Properties

Although dependent properties do not store values, you can still define set methods for them. The set methods cannot set the value of a dependent property, but they can execute other code.

For example, propertyChange is a value class that initially defined a property named OldPropName. You can use a set method to change the property name from the perspective of a class user:

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

Code that accesses OldPropName continues to work as expected, and making OldPropName hidden helps prevent new users from seeing the old property name.

For example, create an instance of propertyChange. Set the property value using the old property name and then display the object. MATLAB sets the value to the property with the new name and displays it.

a = propertyChange; a.OldPropName = "hello"

a =

propertyChange with properties:

NewPropName: "hello"

See Also

Topics