| Contents | Index |
| On this page… |
|---|
The MATLAB Handle 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 hgsetget subclass of the handle class provides implementations of these methods. Derive your class from hgsetget 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. |
Classes inherit set and get methods from hgsetget:
classdef MyClass < hgsetgetBecause hgsetget derives from the handle class, MyClass is also a handle class.
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.
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 hgsetget for an example.
This sample class defines a set/get interface and illustrates the behavior of the inherited methods:
classdef LineType < hgsetget % subclass hgsetget properties Style = '-'; Marker = 'o'; end % Public properties properties (SetAccess = protected) Units = 'points'; end % Protected SetAccess 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(m,'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')
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 hgsetget class:
set(h,'Style','-.-') Error using LineType>LineType.set.Style Invalid line style
Using the set and get methods that are inherited from hgsetget 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 styleSee Property Access Methods for more information on property access methods.
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 sturct S. set cannot return possible values for the properties.
Suppose you create an array of LineType objects:
H = [LineType('..','z'),LineType('--','q')] H = 1x2 LineType handle 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
UnitsGet the value of the Marker property from the second array element in the SV struct array:
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 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 handle
Properties:
Style: '..'
Marker: 'o'
Units: 'points'
H(2)
ans =
LineType handle
Properties:
Style: '--'
Marker: 'x'
Units: 'points'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 | Controlling the Number of Instances | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |