Why in the following class the members are not initialised?

1 vue (au cours des 30 derniers jours)
Simeon
Simeon le 28 Fév 2013
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 commentaires
Daniel Shub
Daniel Shub le 28 Fév 2013
While it is not your problem, your constructor is wrong. It should be obj.value = 0;
Simeon
Simeon le 28 Fév 2013
Hi thanks,
this was just a typo.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 28 Fév 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 commentaires
Simeon
Simeon le 28 Fév 2013
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?
Matt J
Matt J le 28 Fév 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)

Connectez-vous pour commenter.

Plus de réponses (1)

Daniel Shub
Daniel Shub le 28 Fév 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 commentaires
Simeon
Simeon le 28 Fév 2013
How do you vote?
Daniel Shub
Daniel Shub le 28 Fév 2013
Click on the triangle next to "0 votes" underneath the avatar.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Class File Organization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by