Matlab does not really have any established real equivalent to a C pointer available to Matlab programmers. I have never really digged into how matlab was initially organized but to me it seems like transparent memory allocation/deallocation would need to get rid of pointers as these are direct link to the raw memory.
The reasons I am talking about Pointers is that I want to discuss on the usage of Handles in Matlab. And to me, the Handle in Matlab is just like a C Pointer. Handles are actually more complicated than C Pointers but we will come back to this analogy.
First, you might not have noticed but handles are everywhere in Matlab. Surprisingly, a standard course on Matlab will not talk at all about handles even if this is the very first thing you will use when learning Matlab.
Let’s Consider the following lines.
figure(1); x=1:100; y=x.^2; plot(x,y);
Now, this is probably some of the very first piece of code anybody learning Matlab would do. Simple and powerful. In 4 lines you have a very nice graph in a window on your desktop. These 4 lines might look simple but in the background Matlab is doing A LOT of things. Some of these background events are actually quite complex and abstract.
Let us go through these 4 lines in details.
To start, I am sure you know figure is here to open this window on which your plot is going to be. I don’t know if you had the same feeling but when I was taught Matlab, I was kind of bothered by this first line. What is this 1 in figure(1)? And then I was even more bothered. If you do this :
Matlab kicks you out with this :
Error using figure
Requested figure handle in use by another object
What is going on?
This is, I believe, a very good introduction to the Handle object.
Grossly, there are two types of objects in Matlab :
- Value objects
- Handle objects
Values objects are objects that you can copy around without keeping the original reference. For instance in :
a=1; b=a; a=2; b
a is a copy of b. But whatever you do to a, b will stay the same. b has lost its reference to a. When we initiated x and y in our previous code, we created Value objects.
Now handles are behaving like a C pointer. But for some reasons inherent to its construction, Matlab doesn’t really want you to use handles on actual data. But they are used extensively when it comes to interface. This makes sense, you don’t usually want to copy around figure window everywhere. You just want to keep a reference to it. Now back to our first line. When you do figure(1), you tell Matlab to create a figure window and you tag it with the number 1. In other words, you give it the UNIQUE handle of 1. If you were to do again figure(1), the same exact window will pop out. Another way to use figure is to do :
This tells Matlab to create a new figure and put in h, its handle (or its pointer if you want). Now h might looks just like a number but it comes with a lot a hidden capabilities or methods. To see these hidden capabilities, just do :
You will get this :
Alphamap = [ (1 by 64) double array]
CloseRequestFcn = closereq
Color = [0.8 0.8 0.8]
Colormap = [ (64 by 3) double array]
CurrentAxes = 
CurrentObject = 
CurrentPoint = [0 0]
DockControls = on
IntegerHandle = on
InvertHardcopy = on
MenuBar = figure
NextPlot = add
NumberTitle = on
PaperUnits = inches
PaperOrientation = portrait
PaperPosition = [0.25 2.5 8 6]
PaperPositionMode = manual
PaperSize = [8.5 11]
PaperType = usletter
Pointer = arrow
PointerShapeCData = [ (16 by 16) double array]
PointerShapeHotSpot = [1 1]
Position = [440 378 560 420]
Renderer = None
RendererMode = auto
Now that is a lot of information carried by such a small number. Matlab is showing you all the properties of the handle h. In that example, this is the properties of your figure window. I am sure you have notice the position of the window on the screen for instance. To access these properties individually you can use the get and set functions that are inherent methods of this particular class of Handles.
Like so :
In this simple line, you have filled the variable PositionWindowOnScreen with the position in pixels of your figure window. Now why is this analogous to a pointer. Because of the following :
hCopy=h; set(h,'Position',[0 0 100 100]); PositionhCopy=get(hCopy,'Position');
Using the set method to change the position of the window on the screen actually move your figure. And even if you made of copy of h, hCopy is just referenced to the same window so PositionhCopy will give you the actual position of the figure, not the old one, just like a C Pointer.
Now why figure(0) was buggy? Well because, “there can be only one”, and the handle 0 already existed. 0 is the Root handle. The handle to the Matlab session. The only one you can’t delete with delete(h). And very nicely, the Position property of the handle 0 is actually the full screen size!
Before I comment on the following lines (2-3) of our first code, I will need to talk a little bit on the Copy-On-Write mechanisms but that is for another day. You will see that there are even more hidden Pointers in Matlab.
The only thing you have to remember is that every single object on your screen has its handle! And all handles are organized in a tree hierarchy like here :