打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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 :
1
2
3
4
5
6
7
8
9
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 :
01
02
03
04
05
06
07
08
09
10
11
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 :
01
02
03
04
05
06
07
08
09
10
11
classdef HandleObject < handle
properties
Object=[];
end
methods
function obj=HandleObject(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 :
1
2
3
4
5
6
7
8
9
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…
Did you know it was so easy to use pointers in Matlab?
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
浅谈在Swift中关于函数指针的实现
How to Use the restrict Qualifier in C
Autodesk 的 c 题(2007.05) - 面试总结 - 坚持到底
Learn C++ – Skill up with our free tutorials
std::shared_ptr (转)
A Basic Glance At The Virtual Table
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服