Implementing a Set/Get Interface for Properties

The Standard Set/Get Interface

The MATLAB® graphics system implements an interface based on set and get methods. These methods enable you to set or query the values of graphics object properties. The matlab.mixin.SetGet subclass of the handle class provides implementations of these methods. Derive your class from matlab.mixin.SetGet to obtain similar set and get functionality.

    Note:   The set and get methods referred to in this section are different from property set access and property get access methods. See Property Access Methods for information on property access methods.

Subclass matlab.mixin.SetGet

Classes inherit set and get methods from matlab.mixin.SetGet:

classdef MyClass < matlab.mixin.SetGet

Because matlab.mixin.SetGet derives from the handle class, MyClass is also a handle class.

Get Method Syntax

Get the value of an object property using the object handle, h, and the property name:

v = get(h,'PropertyName');

If you specify an array of handles with a single property name, get returns the current property value for each object in H as a cell array of values, (CV):

CV = get(H,'PropertyName');

The CV array is always a column regardless of the shape of H.

When prop is a cell array of string property names and H is an array of handles, get returns a cell array of values where each row in the cell corresponds to an object in H and each column in the cell corresponds to a property in prop. get returns the corresponding property values in an m-by-n cell array, where m = length(H) and n = length(prop)

prop = {'PropertyName1','PropertyName2'};
CV = get(H,prop);

If you specify a handle array, but no property names, get returns a struct array in which each structure in the array corresponds to an object in H. Each field in the structure corresponds to a property defined by the class of H. The value of each field is the value of the corresponding property. If you do not assign an output variable, then H must be scalar.

SV = get(H);

See Using Handle Arrays with Get for an example.

Set Method Syntax

The set method assigns the value of the specified property for the object with handle H. If H is an array of handles, MATLAB assigns the property value to the named property for each object in the array H.

set(H,'PropertyName',PropertyValue)

You can pass a cell array of property names and a cell array of property values to set:

set(H,{'PropertyName1','PropertyName2'},...
   {Property1Value,Property2Value})

If length(H) is greater than one, then the property value cell array can have values for each property in each object. For example, if length(H) is 2 (two object handles), then you can use an expression like this:

set(H,{'PropertyName1','PropertyName2'},...
   {Property11Value,Property12Value;Property21Value,Property22Value})

The preceding statement is equivalent to the follow two statements:

set(H(1),'PropertyName1',Property11Value,'PropertyName2',Property12Value)
set(H(2),'PropertyName1',Property21Value,'PropertyName2',Property22Value)

If you specify a scalar handle, but no property names, set returns a struct array with one field for each property in the class of H. Each field contains an empty cell array.

SV = set(h);

See Class Derived from matlab.mixin.SetGet for an example.

Class Derived from matlab.mixin.SetGet

This sample class defines a set/get interface and illustrates the behavior of the inherited methods:

classdef LineType < matlab.mixin.SetGet
   properties 
      Style = '-';
      Marker = 'o';
   end 
   properties (SetAccess = protected)
      Units = 'points';
   end 
   methods 
      function obj = LineType(s,m)
         if nargin > 0
            obj.Style = s;
            obj.Marker = m;
         end
      end % LineType
      function obj = set.Style(obj,val)
         if ~(strcmpi(val,'-') ||... 
               strcmpi(val,'--') ||... 
               strcmpi(val,'..'))
            error('Invalid line style ')
         end
      obj.Style = val;
      end % set.Style
      function obj = set.Marker(obj,val)
         if ~isstrprop(val,'graphic')
            error('Marker must be a visible character')
         end
         obj.Marker = val;
      end % set.Marker
   end % methods
end % classdef

Create an instance of the class and save its handle:

h = LineType('--','*');

Query the value of any object property using the inherited get method:

get(h,'Marker')
ans =

*

Set the value of any property using the inherited set method:

set(h,'Marker','Q')

Property Access Methods Are Called

MATLAB calls any property access methods (set.Style or set.Marker in the LineType class) when you use the set and get methods that are inherited from the matlab.mixin.SetGet class:

set(h,'Style','-.-')
Error using LineType>LineType.set.Style
Invalid line style

Using the set and get methods that are inherited from matlab.mixin.SetGet invokes any existing property access methods that would execute when assigning or querying property values using dot notation:

h.Style = '-.-';
Error using LineType>LineType.set.Style
Invalid line style

See Property Access Methods for more information on property access methods.

Listing All Properties

You can create a struct containing object properties and their current values using get with only a handle array as input.

For example, the struct SV contains fields whose names correspond to property names. Each field contains the current value of the respective property.

% Create a LineType object and save its handle
h = LineType('--','*');
% Query the property values of object h
SV = get(h)
SV =

     Style: '--'
    Marker: '*'
     Units: 'points'

Create a struct containing the properties that have public SetAccess using set with an object handle:

% Query setable property values
S = set(h)

S =

     Style: {}
    Marker: {}

The LineType class defines the Units property with SetAccess = protected. Therefore, S = set(h) does not create a field for this property in the struct S. set cannot return possible values for the properties.

Using Handle Arrays with Get

Suppose you create an array of LineType objects:

H = [LineType('..','z'),LineType('--','q')]

H = 

  1x2 LineType with properties:

    Style
    Marker
    Units

When H is an array of handles, get returns a (length(H)-by-1) cell array of property values:

CV = get(H,'Style')
CV = 

    '..'
    '--'

When H is an array of handles and you do not specify a property name, get returns a struct array containing fields with name corresponding to property-names. You must assign the output of get to a variable when H is not scalar.

% Assign output of get for nonscalar H
SV = get(H)

SV = 

2x1 struct array with fields:
    Style
    Marker
    Units

Get the value of the Marker property from the second array element in the SV struct array:

SV(2).Marker

ans =

q

Handle, Property Name, and Property Value Arrays

You can pass an array of handles, a cell array of property names, and a cell array of property values to set. The property value cell array must have one row of property values for each object in H and each row must have a value for each property in the property name array:

H = [LineType('..','z'),LineType('--','q')];
set(H,{'Style','Marker'},{'..','o';'--','x'})

The results of this call to set is:

H(1)

ans = 

  LineType with properties:

     Style: '..'
    Marker: 'o'
     Units: 'points'

 H(2)

ans = 

  LineType with properties:

     Style: '--'
    Marker: 'x'
     Units: 'points'

Customizing the Property List

Customize the way property lists display by redefining the following methods in your subclass:

  • setdisp — When you call set with no output argument and a single scalar handle input, set calls setdisp to determine how to display the property list.

  • getdisp — When you call get with no output argument and a single scalar handle input, get calls getdisp to determine how to display the property list.

Was this topic helpful?