| MATLAB® | ![]() |
| On this page… |
|---|
The MATLAB® Handle Graphics® system implements an interface based on set and get methods that 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 and provide the same functionality for instances of your class.
Note The set and get methods referred to in this section are different from the property set access and property get access methods that you can implement to control what happens when any attempt is made to set or query a property value. See Controlling Property Access for information on property access methods. |
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 it is interpreted as a property name and 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.
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, then set returns a cell array of possible values for each requested property, when the property value is restricted to 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 and with each field containing 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 in which each cell corresponds to a property in prop and each cell's value is a cell array of possible values or the empty cell 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.
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 =
500You can set the value of any property using the inherited set method:
set(h,'AccountStatus','closed') ??? Error using ==> myAccount.myAccount>myAccount.set.AccountStatus at 19 Invalid value for AccountStatus
The property set function (set.AccountStatus) is called when you use the set method:
set(h,'AccountStatus','frozen')
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 object's setable properties using set:
>> set(h)
AccountNumber: {}
AccountBalance: {}
AccountStatus: {}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.
![]() | Finding Handle Objects and Properties | Building on Other Classes | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |