Why in the following class the members are not initialised?
Show older comments
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
Matt J
on 28 Feb 2013
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
Daniel Shub
on 28 Feb 2013
While it is not your problem, your constructor is wrong. It should be obj.value = 0;
Simeon
on 28 Feb 2013
Accepted Answer
More Answers (1)
Daniel Shub
on 28 Feb 2013
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
Simeon
on 28 Feb 2013
Daniel Shub
on 28 Feb 2013
Edited: Daniel Shub
on 28 Feb 2013
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.
Simeon
on 28 Feb 2013
Daniel Shub
on 28 Feb 2013
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.
Daniel Shub
on 28 Feb 2013
Note that the best way to show "thanks" is by voting and accepting answers
Simeon
on 28 Feb 2013
Daniel Shub
on 28 Feb 2013
Click on the triangle next to "0 votes" underneath the avatar.
Categories
Find more on Custom Toolchain Registration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!