Implementing a Set/Get Interface for Properties
The Standard Set/Get Interface
The MATLAB Handle Graphics system implements an interface based on set and get methods. These methods enable you to set or query the value of graphics object properties. The hgsetget subclass of the handle class provides implementations of these methods that your class can inherit to provide the same set and get functionality.
[warning]The set and get methods referred to in this section are different from the property set access and property get access methods. See Controlling Property Access for information on property access methods.[/warning]
Property Get Method
The get method returns property values from a handle array.
function SV = get(H)
function CV = get(H,prop)
- If you do not specify property names, get returns a struct array in which each element corresponds to the element in H. Each field in the struct corresponds to a property defined by the class of H. The value of each field is the value of the corresponding property.
- If you specify prop as a char array, then MATLAB interprets it as a property name. get returns the value of that property if H is scalar, or returns a cell array of property values if H is an array of handles. The cell array is always a column vector regardless of the shape of H. If prop is a cell array of string property values, then get returns a cell array of values where each row in the cell corresponds to an element in H and each column in the cell corresponds to an element in prop.
Property Set Method
The set method assigns values to properties for handles in array H.
function S = set(H)
function info = set(H, prop)
function set(H,'PropertyName',PropertyValue)
If you do not specify property values, set returns a cell array of possible values for each requested property when the property value is a finite enumeration of possible values.
- If you specify only H, set returns a struct with one field for each property in the class of H. Each field contains either an empty cell array or a cell array of possible property values (if such a finite set exists).
- If you specify prop as a string containing a property name, then set returns either a cell array of possible values or an empty cell.
- If you specify prop as a cell array of property names, then set returns a cell column vector. Each cell corresponds to a property in prop and each cell value is a cell array of possible values, or empty if there is no finite enumeration of possible values.
You can also pass property-value pairs to set using cell arrays and structures as described for the built–in set function.
Subclassing hgsetget
This example creates a class with the set/get interface and illustrates the behavior of the inherited methods:
classdef MyAccount < hgsetget % subclass hgsetget
properties
AccountNumber
AccountBalance = 0;
AccountStatus
end % properties
methods
function obj = MyAccount(actnum,intamount,status)
obj.AccountNumber = actnum;
obj.AccountBalance = intamount;
obj.AccountStatus = status;
end % MyAccount
function obj = set.AccountStatus(obj,val)
if ~(strcmpi(val,'open') ||...
strcmpi(val,'deficit') ||...
strcmpi(val,'frozen'))
error('Invalid value for AccountStatus ')
end
obj.AccountStatus = val;
end % set.AccountStatus
end % methods
end % classdef
Create an instance of the class and save its handle:
h = MyAccount(1234567,500,'open');
You can query the value of any object property using the inherited get method:
get(h,'AccountBalance')
ans =
500
You can set the value of any property using the inherited set method:
set(h,'AccountStatus','frozen')
MATLAB calls the property set function (set.AccountStatus) when you use the set method:
set(h,'AccountStatus','closed')
??? Error using ==> MyAccount.MyAccount>MyAccount.set.AccountStatus at 19
Invalid value for AccountStatus
Listing All Properties
The standard set/get interface enables you to display all object properties and their current values using get with no output argument and only a handle as input. For example,
get(h)
AccountNumber: 1234567
AccountBalance: 500
AccountStatus: 'open'
Similarly, you can list the setable properties of the object using set:
set(h)
AccountNumber: {}
AccountBalance: {}
AccountStatus: {}
Customizing the Property List
You can customize the way property lists are displayed by redefining the following methods in your subclass:
- setdisp — Called by set when you call set with no output arguments and a single input parameter containing the handle array.
- getdisp — Called by get when you call get with no output arguments and a single input parameter containing the handle array.