Why in the following class the members are not initialised?

Dear all, I am just starting with Matlab classes and being a C++ developer for more than a decade, I really have a hard time understanding how classes work in MATLAB. I have run into the following problem that can be easily reproduced by this simple class.
classdef Simple
properties
value
end
methods
function obj = Simple()
value = 0;
end
function make(obj, val)
obj.value = val;
fprintf('initializing value to %f\n', obj.value);
end
end
end
Once you place it in the @Simple directory as @Simple/Simple.m and start Matlab try the following:
S=Simple;
S.make(10)
S
You will see that although in the member function name the member is initialised properly, when the member function returns the S.value is no more equal to 10. What is the problem?

3 Comments

Your constructor needs to initialize obj.value, not "value" alone.
function obj = Simple()
obj.value = 0;
end
Alternatively, you could have just intialized in the properties block
properties
value=0
end
While it is not your problem, your constructor is wrong. It should be obj.value = 0;
Hi thanks,
this was just a typo.

Sign in to comment.

 Accepted Answer

You need to return obj from make()
function obj=make(obj, val) %<-----RETURN AN OUTPUT ARG
obj.value = val;
fprintf('initializing value to %f\n', obj.value);
end

3 Comments

Or you can make Simple a handle class
classdef Simple<handle
Then you don't have to return obj from make(). However, you should be sure to read up on the differences in behavior between handle classes and value classes.
The first one of the solutions you suggested doesn't work. Try it yourself. The second one does the job, but I do not understand why. Could you please explain why the first one of your suggestions doesn't work and why the second one works?
Just to summarize what Sean said, you must also (for value classes) return an output in the line of code that calls the method.
S = S.make(10)

Sign in to comment.

More Answers (1)

I am going to build on Matt's answer ...
Your problem is that "objects" in MATLAB are generally what are refer to as value classes. This means that
x = 1;
y = x;
results in two distinct objects of class double that are equal, but not identical. You can x without changing y. While MATLAB is smart about memory management, you can conceptualize x and y as occupying different bits of memory.
If we expand on the example and do
plus(x, y);
we create a new hidden object ans of class double (MATLAB silently creates and overwrites ans whenever it needs to) that holds the result of the plus method. Calling the plus method has no effect on either the x or y objects. If instead we did
x = plus(x, y);
then we are obviously replacing the current value of x with the result of the plus method. You make method fails because it doesn't return anything. If you use Matt's answer, and modify your make method to return an object of class Simple, then it will behave just like
plus(x, y);
and
x = plus(x, y);
The other way to solve the problem is to not use value classes, but rather handle classes. An example of a handle class is audioplayer objects (graphics objects are also like handle classes, but are a little different).
x = audioplayer(0.1*randn(44.1e3, 2), 44.1e3);
y = x;
now x and y are identical and both point to the same object. If we change x, we change y.
isplaying(y), play(x); isplaying(y)
You can find out more in depth and better descriptions in the documentation about value and handle classes.

7 Comments

Unfortunately what Matt suggested doesn't work. Even if I redesign my make function as:
function obj = make(obj, val)
obj.value = val;
fprintf('initializing value to %f\n', obj.value);
end
still doesn't work. Once the function returns S.value is not 10 but zero. Try it and you will see. Thanks for the clarification of handle.
There is a difference between
a = Simple;
a.make(10);
b = ans;
a.value
b.value
and
c = Simple;
c = c.make(10);
c.value
As I would expect a.value is equal to 0 and b.value and c.value are equal to 10, what do you mean by it doesn't work? This is the difference I was trying to get at with my plus example. Your modified make method returns an object of class Simple with a value property equal to 10. In the first example that object is placed into the dummy variable ans since you do not specify where to save it.
OK so this is the MATLAB way... Every time I want to call the function of an object to operate on its arguments I need to do it as if the function acts like a mathematical operator? This the C++ way of overloading operators (+, -, =, *) which return an object of the same type. I could have never imagined something like that. Thanks for the hint.
If you are using a value class, then yes, but if you use a handle class, than you can get the behavior you are expecting.
Note that the best way to show "thanks" is by voting and accepting answers
Click on the triangle next to "0 votes" underneath the avatar.

Sign in to comment.

Categories

Asked:

on 28 Feb 2013

Community Treasure Hunt

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

Start Hunting!