Why in the following class the members are not initialised?

1 回表示 (過去 30 日間)
Simeon
Simeon 2013 年 2 月 28 日
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 件のコメント
Daniel Shub
Daniel Shub 2013 年 2 月 28 日
While it is not your problem, your constructor is wrong. It should be obj.value = 0;
Simeon
Simeon 2013 年 2 月 28 日
Hi thanks,
this was just a typo.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2013 年 2 月 28 日
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 件のコメント
Simeon
Simeon 2013 年 2 月 28 日
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 2013 年 2 月 28 日
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)

サインインしてコメントする。

その他の回答 (1 件)

Daniel Shub
Daniel Shub 2013 年 2 月 28 日
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 件のコメント
Simeon
Simeon 2013 年 2 月 28 日
How do you vote?
Daniel Shub
Daniel Shub 2013 年 2 月 28 日
Click on the triangle next to "0 votes" underneath the avatar.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeClass File Organization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by