MATLAB: Is it possible to make objects of the same class listen to each other in MATLAB 7.7 (R2008b)

MATLAB

I have two instantiations of the same class. I want one instance to listen to the other instance. Is this possible?

Best Answer

Yes, it is possible to create two instances of the same class and set it up so that one listens to the other. The key is in how you define the instances.
If you define the instances 'a' and 'b' as follows, you are creating two different instances of type 'myclass', where 'b' listens to 'a'.
a = myclass;
b = myclass(a)
If you define the instances 'a' and 'b' as follows, you are creating one instance of 'myclass'. Any changes to 'b' will affect 'a', and vice versa.
a = myclass;
b = a;
b = myclass(a);
Please see the resolution document myclass.m for the class definition used in the above examples. Run the following example code to step through the class definition:
a = myclass; % creates one instance of myclass
b = myclass(a); % creates another instance of myclass that listens to 'a'
changestate(a,4);% changes the state of 'a' causing 'b' to react