Why in the following class the members are not initialised?
1 view (last 30 days)
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
Daniel Shub
on 28 Feb 2013
While it is not your problem, your constructor is wrong. It should be obj.value = 0;
Accepted Answer
Matt J
on 28 Feb 2013
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
Matt J
on 28 Feb 2013
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)
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
See Also
Categories
Find more on Class File Organization 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!