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 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.

Property Get Method

The get method returns property values from a handle array.

function SV = get(H)
function CV = get(H,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, 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.

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','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')

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 object's setable properties 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:

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS