OOP in Matlab: Why can't methods access properties directly?

12 views (last 30 days)
I have a fundamental problem with Matlab. I just want to make sure that I understand the problem correctly.
Fact 1:Matlab sends arguments to functions BY-VALUE by default. Fact 2:Matlab class methods, don't have direct access to the class properties, and the object is sent as the first argument to the method.
Fact 1 + Fact 2: methods can't change the properties! They can return a copy of the object with the property changed. But the original object remains unchanged!!
What I'm trying to accomplished and can't is the following:
I have a class which is a set of points. I want to implement a method called "add" and I want to run it as: SetOfPoints1.add(Point1);
not as SetOfPoints1 = SetOfPoints1.add(point1);
why? because adding a point to the matrix of points demands extending the size of the matrix and getting a copy etc. I don't wanna get a copy twice!! It slows everything down specially when I have too many points in my set.
I know about handle/reference classes. But I'm reluctant to use it since I don't know what happens under the hood. But if it's the only way to go I'm gonna give it a try.
One more thing, in OOP , aren't methods supposed to work on the properties directly? I mean, isn't that a fundamental assumption about OOP? doesn't returning a copy violate encapsulation ? .. I don't want a copy of my object to be returned every time I run a method!!
Thanks, Aidin
  1 Comment
per isakson
per isakson on 14 Sep 2011
Edited: per isakson on 7 May 2016
In Matlab there are value objects, which are immutable, and handle objects, which are mutable. That's the way The Mathworks designed it. Handle objects behave the way you suppose OOP to behave.
Be careful to draw conclusions about the implementation based on the interface.
There are performance issues, which have been discussed here and in the newsgroup, CSSM. I quess The Mathworks works on it.

Sign in to comment.

Accepted Answer

Andrew Newell
Andrew Newell on 14 Sep 2011
If I understand your problem correctly, you could make SetOfPoints into an object array, where each element ( e.g., SetOfPoints(1)) is a point (see Creating Object Arrays). Then, if you want a method add, you could implement it by using a hidden property that keeps track of the current number of points (there are a lot of other ways to do it).
As for slowing down the program, your problem is really no different than growing any other array. To avoid slowdown, pre-allocate the elements of the array.
EDIT: in your example below, MATLAB is not necessarily copying all those matrices because it has a lot of internal optimization (see Can MATLAB pass by reference?) However, if you want to be sure this is not happening, you will need to use handle classes.
As for why any particular feature is designed that way, the people on MATLAB Answers are just users - we didn't write the software.
  1 Comment
Aidin Foroughi
Aidin Foroughi on 14 Sep 2011
Edited: per isakson on 7 May 2016
Well, this is true. But I have other classes too. For instance, I have a class with large matrices in it. KalmanFilter. Then I want to call a method to do some work on the matrices in the KalmanFilter. What happens is that I have to return the whole kalmanfilter and re-assign it.
for instance:
k = kalmanfilter(Q,R,A etc etc) % creating and object
% then if I if I want to call a method in it I'll have to do:
k = k.predict();
%not simply
k.predict();
I want predict to manipulate the properties inside k. I dont want to copy the whole thing!...
If I understand correctly, when I call k.predict() first a copy of the kalmanfilter with all those huge matrices is sent to predict() then predict does something on them and then I have to copy it back to k again. This is so inefficient!! .. why can't predict() (the method) access the properties of it's own class directly (by reference )and not BY_VALUE. ?
this is the case with allll OOP languages because it is assumed that methods work on properties...
Thanks, Aidin

Sign in to comment.

More Answers (1)

Daniel Shub
Daniel Shub on 14 Sep 2011
Fact 1:Matlab sends arguments to functions BY-VALUE by default.
This is not really true. While MATLAB does not pass by reference, it also does not pass by value. Rather it uses a complicated and generally effcient copy on chage strategy. Since it is a propritery process (and ever changing process), it is not really advisable to optimize against it.
There is no reason you cannot do:
k = kalmanfilter(Q,R,A, ...)
k.predict();
you just need to make the kalmanfilter class a handle class. Since MATLAB is doing something more efficient then pass by value, this is not so inefficent.
Handle and value classes serve different roles and if you want to use OOP in MATLAB it is worth learning the difference.
  1 Comment
Aidin Foroughi
Aidin Foroughi on 14 Sep 2011
Thanks for your response. I didn't know about efficient processes happening under the hood. But what is obvious is that if the original properties of the object passed to the method are not changed the should be kept somewhere and therefore some unnecessary copying is being done at some point.
I knew about the handle classes. The reason I was reluctant to use a handle class was to keep consistency. Now I should take extra care as to where I should call methods as in k.predict() and where as k = k.predict(). If I do the former when I'm supposed to do the latter I see no change in my values and no warnings or errors to show for it ...
Anyways, I believe handle classes are the way to go.
Thanks for your quick and helpful response.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!