Implement Set/Get Interface for Properties

The Standard Set/Get Interface

Some MATLAB® objects, such as graphics objects, implement an interface based on set and get functions. These functions enable access to multiple properties on arrays of objects in a single function call.

You can add set and get functionality to your class by deriving from one of these classes:

  • matlab.mixin.SetGet — use when you want support for case-insensitive, partial property name matching. Deriving from matlab.mixin.SetGet does not affect the exact property name required by the use of dot notation reference to properties.

  • matlab.mixin.SetGetExactNames — use when you want to support only case-sensitive full property name matching.


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 Syntax

Use the abstract class matlab.mixin.SetGet or matlab.mixin.SetGetExactNames as a superclass:

classdef MyClass < matlab.mixin.SetGet

Because matlab.mixin.SetGet and matlab.mixin.SetGetExactNames derive from the handle class, your subclass is also a handle class.

Get Method Syntax

The get method returns the value of an object property using the object handle and the property name. For example, assume H is the handle to an object:

v = get(H,'PropertyName');

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

CV = get(H,'PropertyName');

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

If you specify a cell array of char vector property names and an array of handles, get returns a cell array of property values. Each row in the cell corresponds to an object in the handle array. Each column in the cell corresponds to a property name.

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

get returns an m-by-n cell array, where m = length(H) and n = length(props).

If you specify a handle array, but no property names, get returns an array of type struct in which each structure in the array corresponds to an object in H. Each field in each structure corresponds to a property defined by the class of H. The value of each field is the value of the corresponding property.

SV = get(H);

If you do not assign an output variable, then H must be scalar.

For an example, see Using get with Arrays of Handles.

Set Method Syntax

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


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

props = {'PropertyName1','PropertyName2'};
vals = {Property1Value,Property2Value};

If length(H) is greater than one, then the property value cell array (vals) can have values for each property in each object. For example, suppose length(H) is 2 (two object handles). You want to assign two property values on each object:

props = {'PropertyName1','PropertyName2'};
vals = {Property11Value,Property12Value;Property21Value,Property22Value};

The preceding statement is equivalent to the follow two statements:


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

SV = set(h);

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
      Style = '-'
      Marker = 'o'
   properties (SetAccess = protected)
      Units = 'points'
      function obj = LineType(s,m)
         if nargin > 0
            obj.Style = s;
            obj.Marker = m;
      function obj = set.Style(obj,val)
         if ~(strcmpi(val,'-') ||...
               strcmpi(val,'--') ||...
            error('Invalid line style ')
         obj.Style = val;
      function obj = set.Marker(obj,val)
         if ~isstrprop(val,'graphic')
            error('Marker must be a visible character')
         obj.Marker = val;

Create an instance of the class and save its handle:

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

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

ans =


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


Property Access Methods Called with set and get

MATLAB calls property access methods (set.Style or set.Marker in the LineType class) when you use the set and get methods.

Error using LineType>LineType.set.Style
Invalid line style

For more information on property access methods, see Property Access Methods

List All Properties

Return a struct containing object properties and their current values using get:

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

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

Return a struct containing the properties that have public SetAccess using set:

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 Units in S.

set cannot return possible values for properties that have nonpublic set access.

Using get with Arrays of Handles

Suppose that you create an array of LineType objects:

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

  1x2 LineType with properties:


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 names corresponding to property-names. Assign the output of get to a variable when H is not scalar.

SV = get(H)
SV = 

2x1 struct array with fields:

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

ans =


Arrays of Handles, Names, and Values

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. Each row must have a value for each property in the property name array:

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

The result of this call to set is:

ans = 

  LineType with properties:

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

  LineType with properties:

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

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

See Also


Related Topics