Using pointers in Matlab

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 :

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 :

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 :

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.

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…

Now the moment the Modify() function tries to modify
thing_handle.Object, a new copy of thing will be created
due to copy-on-write. So this would always lead to at least
one copy of the object being made. If the object is large
then this could be inefficient.

I don’t know if the matlab interpreter is smart enough not
to make a copy if it determines that the original thing is never
used after being passed to HandleObject. It would be
safer to assume that the interpreter is not so smart and
in that case, to get around this, one would need to write:

then you could write:
thing_handle = CreateHandle(‘SomeThing’, ….contructor arguments…);
thing_hande.Object.Modify();

BTW, I like Yair Altman’s “Undocumented ..” blog, but it is a bit too
Java oriented for me. I’ve not come across any another that delves
a little bit into the inner workings of Matlab. So your blog is much
needed. If its not asking too much, perhaps you could write something
about the C/C++ interfaces to Matlab, which is where a lot of numerics
people could benefit.

Yes, a post on creating MEX files has been in my mind for some time, also on the deploy process. I am trying to alternate between higher level posts and more beginners posts. The list of things still to cover is long obviously but I am slowly writing my Matlab book-blog.

I wish this worked for the intended use, but it seems to me that matlab objects don’t deal well with large objects in matlab. Every time data is set on or retrieved from an object, a deep copy is made. So if you’re using the class for large data, your access times will be quite slow.