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.
Note:
The |
Use the abstract class matlab.mixin.SetGet
or matlab.mixin.SetGetExactNames
as
a superclass:
classdef MyClass < matlab.mixin.SetGet ... end
Because matlab.mixin.SetGet
and matlab.mixin.SetGetExactNames
derive
from the handle
class,
your subclass is also a handle
class.
The get
method returns the value of an object
property using the object handle and the property name:
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.
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
.
set(H,'PropertyName
',PropertyValue)
You can pass a cell array of property names and a cell array
of property values to set
:
props = {'PropertyName1
','PropertyName2
'}; vals = {Property1Value,Property2Value}; set(H,props,vals)
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}; set(H,props,vals))
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
with one field for each property in the
class of H
. Each field contains an empty cell array.
SV = set(h);
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 function obj = set.Style(obj,val) if ~(strcmpi(val,'-') ||... strcmpi(val,'--') ||... strcmpi(val,'..')) error('Invalid line style ') end obj.Style = val; end function obj = set.Marker(obj,val) if ~isstrprop(val,'graphic') error('Marker must be a visible character') end obj.Marker = val; end end end
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')
MATLAB calls property access methods (set.Style
or set.Marker
in
the LineType
class) when you use the set
and get
methods.
set(h,'Style','-.-')
Error using LineType>LineType.set.Style
Invalid line style
For more information on property access methods, see Property Access Methods
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.
get
with Arrays of HandlesSuppose that 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 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: Style Marker Units
Get the value of the Marker
property from
the second array element in the SV
array of structures:
SV(2).Marker
ans = q
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')]; 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'
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.