Before we move back to Matlab, I am very happy to announce, for the new year, that Matlabtips.com got itself a professional illustrator. We are now well equipped to fulfill our mission : Make learning Matlab easy and entertaining. Among my resolutions for new year, there is one to make tons of new posts (illustrated this time with original drawings!).
As I discussed already several times, Matlab has supposedly no real equivalent to the C pointer available to you. This is so due to a design choice from Mathworks. The entire language is organized to avoid pointers. Even so, there are occasions where a pointer is really what we need, like when you are dealing with very large datasets that take nearly the entire memory. In this post, I first introduce you to the world of pointer and then shows you how to use them in Matlab for real.
But first things first, what is a pointer (folks that know can jump a few lines here) ?
Well, as stated, a pointer points at. A pointer is the address of an object in memory. To explain why this is useful, let’s take a very recent topic : Christmas.
Let’s suppose you bought two gifts to your daughter. A small doll and a trampoline. It is very likely that you are going to give the doll directly to her while you will mention where the trampoline gift is (like : “take a look in the garden”). In other words, you will pass the doll “by value” (the object directly) and the trampoline by reference (where it is). Already you see the advantage : You want to pass things “by reference” when it is too big to be transported around to be passed “by value”.
It’s all the same in programming languages. Some objects can take a very large space in memory and you don’t want this space to be duplicated. Indeed whenever you pass a variable to a function like :
function Parent() GIFT='Doll'; GIFTback=giveDaughter(GIFT); GIFT end function receivedGIFT=giveDaughter(receivedGIFT) receivedGIFT=['dressed ',receivedGIFT]; end
When GIFT is passed to giveDaughter, it is passed by value. Then if it is modified within the function, a copy is made so that the original GIFT in the Parent function is not modified. If you run this example, you will see that GIFT stays ‘Doll’ even after its been modified in giveDaughter. The modified value is stored back in GIFTback.
If you want to avoid this behavior and modify directly the GIFT object in the Parent function, there are several ways.
To do this, you could use global variables. These are variables that are available everywhere in all functions like so :
function Parent() global GIFT; GIFT='Doll'; giveDaughter(); GIFT end function giveDaughter() global GIFT; GIFT=['dressed ',GIFT]; end
But the problem with global is that they are global!
Do you really want every single child in the world to have access to your big GIFT? Of course not, if it is really big, you want to pass it only to who it belongs to.
Another solution would be to use In place computation. But this mechanism is rather obscure and is absolutely not officially documented. Definitely not a nice way to hand a precious GIFT.
The best solution would be to use pointers but how in Matlab?
The answer lies in a tiny bit of Object Oriented Code. Indeed, as I posted already, handle objects are like pointer and luckily for us, Matlab allows you to create new class that inherits all of the properties of the handle class.
But don’t worry, this is very simple Object Oriented code. All you have to do is create a HandleObject.m M-file with this code :
classdef HandleObject < handle properties Object=; end methods function obj=HandleObject(obj,receivedObject) obj.Object=receivedObject; end end end
Place this M-file in a folder where your code is (or under a path that is stored in your matlab paths) and you are ready to go.
I guess this will probably be your first OO code, so let’s explain a little bit what is going on.
classdef HandleObject < handle asks Matlab to create a new class that inherits its property from the handle class (a class of pointers!).
In this class, there is one single property (a property is more or less a variable associated to your object) called Object. The methods HandleObject is being called when you create the object with the argument ‘receivedObject’. This argument is stored in the property Object via the syntax obj.Object.
Now the equivalent of our previous code, using POINTERS would be :
function Parent() GIFT=HandleObject('Trampoline') giveDaughter(GIFT); GIFT.Object end function giveDaughter(receivedGIFT) receivedGIFT.Object=['broken ',receivedGIFT.Object]; end
Please note how I use the ‘.’ to access the underlying object stored in the HandleObject. Also you see the usage of HandleObject(‘Trampoline’) to create (usually this is called the constructor of the class) the object using the first argument.
This time the original trampoline is broken in the Parent hands as well…