2D Graphics: Handle Graphics

MATLAB "Handle Graphics" (HG) refers to the collection of low level graphic routines that actually generate or carry out graphical changes you see in the MATLAB figure window.  For most part these routines are transparent and wrapped by higher level commands.  Knowledge of HG is further made unnecessary by the ability to edit the figure through plot editing commands.  There are at least three reasons why you need to know about them as far as these pages are concerned:

• You wish to control all facets of the figure - the elements, their appearance, their location through the program/code so that there is no need for further interaction
• You wish to generate a sequence of animation
• You need to engage in graphic or image processing
Handle Graphic functions operate through Handles.  All graphic objects in MATLAB are identified by a unique number called a handle. In order to get and set properties of objects it is necessary to know their handle. So it is a good idea to associate a handle with a graphic object.  For example
h = plot(x,y)

will create a plot of y versus x in a figure window with an axis and will return a value for the handle for the plot (line) that will be stored in variable h.  All objects will have default properties.  We can then change the appearance of the plot through this handle variable.  For example

set(h,'Color','r');

will cause the plot to be drawn in red color. All object properties are typically specified as a pair

('PropertyName',Value)
PropertyName is usually a string. Value is an appropriate quantity - string, number,array
There is a hierarchy of graphic objects. Some objects contain other objects.  This concept is associated with parent/children objects.
There are two important functions:
• get(h):    will indicate the current properties and their values of the graphic object whose handle is h.

• Running the following three line code will indicate the current properties of the plot line in the Command window.  As you can see the plot has an impressive set of properties all of which can be changed using the set function

t = 0:0.1:3;
h1 = plot(t,3*exp(-0.5*t).*sin(2*t));
get(h1)

Color = [0 0 1]
EraseMode = normal
LineStyle = -
LineWidth = [0.5]
Marker = none
MarkerSize = [6]
MarkerEdgeColor = auto
MarkerFaceColor = none
XData = [ (1 by 31) double array]
YData = [ (1 by 31) double array]
ZData = []

BeingDeleted = off
ButtonDownFcn =
Children = []
Clipping = on
CreateFcn =
DeleteFcn =
BusyAction = queue
HandleVisibility = on
HitTest = on
Interruptible = on
Parent = [100.001]
Selected = off
SelectionHighlight = on
Tag =
Type = line
UserData = []
Visible = on

You can also enquire about a particular property instead of all property

get(h1,'Color')    % get the current color of the plot line

ans =
0     0     1   % Blue color
• set(h,'PropertyName1',Value,  ...

•         'PropertyName2',Value,  ...
...
)
set(h1,'Marker','s', ...    % square Marker
'LineStyle','--', ...   % dash line
'LineWidth',2, ...     %  Width 2 points
'MarkerSize', 10, ...  % marker size in points
'MarkerEdgeColor','b', ... % color blue
'MarkerFaceColor','y');    % color yellow
There are two important functions that return the handle of the current figure and axes
get(gcf)     % get the handle of current figure
get(gca)    % get the handle of the current axes