recipes : Plotting : Plot Manipulation : What is a "handle"?

Problem

What is a "handle" of a plot object and what does it do?

Solution

If you want to get fine control over your plots then you're going to want to learn about handles. Fortunately it's not hard, and once you've got the idea there's a lot you can do with it. Let's go through everything step by step.

clf
plot(rand(1,100)) %Make a plot with some random data

%The handle for the figure window
>> F=gcf
F =
   1                                                                                                             

%The handle for the axes
>> A=gca                                                                                                         
A =
  173.0011

The gcf command gets the current figure handle. gca does the same, but for the axes. The variables F and A are known as "handles" that refer to this particular figure and these particular axes respectively. They work rather the same way as your home address, which refers to the building in which you live. I have not used a semicolon on the end of those lines in order to show you what the handles are: they're basically just numbers that MATLAB knows refer to a particular object. By themseleves these handles aren't very exciting, it's what MATLAB does with them that's so useful.

If you type get(F), you will be presented with a large quantity of information about the figure window you just made. This lists all of the properties of the window and all of the stuff that you can modify should you want to. Let's explore this with an example.

%We can get information about just one property as follows:
get(F,'Color') %Get the figure background color
ans = 
     1  1  1

%That tells us that the figure background color is white. 

set(F,'Color',[1,0,0.5]) %Let's be silly and make it shock-pink

The above is more or less all you need to know about graphics handles. The get command acquires information about a particular property of the plot object and the set command alters it. There's one more useful piece of information: if you call set with only a handle propery name then it tells you what that propery is and what values it can be set to. Try running set(F,'Toolbar'). It will inform you (somewhat tersely) that set(F,'Toolbar','none') will allow you to remove the toolbar.

What about the handle for the axes, what can we do with that? Obviously you can try get(A) and see what can be modified and have a play with that. But there's one more thing that's worth learning: child objects.

In my example, the value for the axes handle is 173.0011 (yours will be different, that's ok). Try typing get(F,'Children'), which returns the handles of all "child" objects of object F. This will return the same value returned by gca (the axes handle ID). Each plot object may have one of more child objects nested within it. Here, the current figure (F) contains one axis (A) which is its child. The axes also have a child object. Let's play with that.

C=get(A,'Children');

%Ah! So this is the handle for the line on the plot
>> get(C,'Type')
ans =
line

%Let's change the color:
set(C,'Color',[1,0,0.5])

%The plot now looks really terrible. :-/
Discussion

Learning to manipulate plot objects gives you a lot of power in making plots. It means that you can customise your figures programatically, within in a script or function. You don't therefore have to mess around with the menus and click on plot elements, etc. If you're making a figure for publication then you can put all the code required to make that figure into a script. In the future you know that re-running that script will reproduce your figure. This makes it easy to document how the figure was made and to modify it again in the future.