Lisning for a subclass property

I have a main class and a subclass and I would like to listen for the change in any of the subclass properties. In other term when the property in the subclass changes, I would like to trigger a function in the main class. I coded it this way but nothing is happening. thanks for your help.
mainclass =
classdef class_mainclass < handle
properties (SetAccess = private)
end
properties (SetObservable)
subclass;
end
events
end
methods
function obj = class_mainclass
obj.subclass = class_subclass;
addlistener(obj,'subclass','PreSet',...
@PushButton.do);
end
function do(src,evt)
display('did');
end
end % methods
end % classdef
and a subclass
classdef class_subclass < handle
properties (SetAccess = private)
end
properties (SetAccess = public)
A=1;
end
events
end
methods
function obj = class_subclass
end
function obj = addA(obj)
obj.A=obj.A+1;
end
end % methods
end % classdef
>> m = class_mainclass;
>> m.subclass.addA

 Accepted Answer

Guillaume
Guillaume on 24 Jun 2015
Edited: Guillaume on 24 Jun 2015
First, a matter of terminology. Your subclass is not a subclass at all. A subclass derives from the main class. In matlab OOP this implies the following definition:
classdef subclass < mainclass
Your subclass is just a standard class that happens to be a member of your main class. It would be very odd for a main class to instantiate a subclass (except through factory methods), anyway.
In any case, If you want to listen to the property changes of your member class, it is these properties you need to set observable and these properties you need to attach a listener. At present, the only thing you'll detect is if your subclass member is changed to a new instance. That is you'll detect this:
m.subclass = class_subclass; %assign a new class_subclass to the member property of m

5 Comments

Thanks for the very quick answer. as you can tell I am new at OOP in Matlab and not familier with syntax as much but wanted to move the project forward. I have previously tried addlistener(obj,'subclass.A','PreSet',... @PushButton.do); but that gave me an error
So not sure what is the correct syntax for checking on A from the mainclass.
The first thing you would have to do is add the SetObservable attribute to the property you want to listen to. In your example you'd modify class_subclass to
properties (SetObservable)
A = 1;
end
You can then listen to it in your other class by changing your addlistener line to
addlistener(obj, subclass, 'A', 'PreSet', @PushButton.do);
You'd have to do the same for the other properties of class_subclass.
I would also change the name of your classes. You don't have main class and subclass. Just two different classes. You also ought the make the subclass property of main class private (and rename that as well).
I implemented your recommendations and I got this error Undefined function or variable 'subclass'.
Error in class_mainclass (line 18) addlistener(obj,subclass,'A','PreSet',@class_mainclass.do);
I would appreciate your help.
Sorry, I made a typo. It should have read:
addlistener(obj.subclass, 'A', 'PreSet', @PushButton.do);
Thanks

Sign in to comment.

More Answers (0)

Categories

Find more on Class Introspection and Metadata in Help Center and File Exchange

Asked:

on 24 Jun 2015

Commented:

on 25 Jun 2015

Community Treasure Hunt

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

Start Hunting!