%% Graphics Handles
%
% Matlab typically makes sensible choice for you when plotting graphics,
% such as defining the axis limits, tick loctations, etc. However, 
% sometimes you want more control. This is possible by using the Graphics 
% Handles.
%
% A Graphics Handle can be though of as a connection to a graphics object 
% (handles are unique identifiers of graphics objects). 
% Graphics objects are arranged in a parent-child inheritence structure.
%
% Most functions that create graphics objects return handles to these 
% (floating point numbers). Save this number in order to manipulate the 
% object now or later. 
%
% Note, the root object has the handle 0. There is only one root and it is 
% created when Matlab starts up. 
%
%% How to get the handles
%
% Functions that draw objects typically return the handles to these
% objects when prompted for an output argument, e.g.
hfig = figure
x = 0:pi/20:2*pi;
hsin = plot(x,sin(x))
hx = xlabel('x')
%%
% There are also specific functions that return the handle to
% particular objects, namely the handle to the current figure (gcf), and
% the handle to the current axes (gca).
gcf
gca
%%
% Now you can use the handles to customize the objects. E.g., make the line
% thicker.
set(hsin,'linewidth',4)
%%
% Linewidth is just one of many properties of the line object. To see all
% properties and the values use "get".
get(hsin)
%%
% Change the value of any property using set(h,'PropertyName',value) or
% find out about the value using get(h,'PropertyName').
%
% For example:
get(gca,'Children')
%%
% or
get(hsin,'type')
%%
% If there are several children get will return a vector of handles. E.g.
figure
plot(x, sin(x))
hold on
plot(x,exp(-0.1*x).*sin(x),'o')
hkids = get(gca,'children')
%%
% The handles to children are returned in the reverse order in which they
% were created. Hence, hkids(1) is the handle to the exponentially decaying
% graph. Let's make the sin thicker and change the marker for the decaying 
% sin.
set(hkids(1),'marker','*')
set(hkids(2),'linewidth',4)
%%
% If things got too complicated in your plot and you can't figure out
% which handle belongs to an object you can try to identify it by a known
% property value using "findobj", e.g.
hdecay = findobj('marker','*')

%% Parenting
%
% By default all graphics objects are created into the current figure and 
% current axes. However, you can specify another parent when you create the
% object. E.g.,
close all
h1 = figure;
h2 = figure;
h3 = figure;
axes('Parent',h1)
axes('Parent',h2,'position',[0.1 0.5 0.5 0.4])
%%
% Axes were created at a predefined position in figure 2 and in default 
% position in figure 1. You can use figure(h1) and figure(h2) to bring
% those figure windows to the foreground.
%
% You can even move an object from one parent to another using set, e.g. to
% move the current axes to figure 3 type
set(gca,'Parent',h3)




