Why are properties stored in "event.PropertyEvent.AffectedObject" instead of objects?
1 view (last 30 days)
Show older comments
The listener executes the callback function when MATLAB® triggers the property event. Define the callback function to have two specific arguments, which are passed to the function automatically when called by the listener:
- Event source — a meta.property object describing the object that is the source of the property event
- Event data — a event.PropertyEvent object containing information about the event
handle object
Object whose property is affected, specified as the object handle.
So,There should be an object stored in the ’-AffectedObject‘.But the result of code execution is not
Here is an example.make a breakingpoint in ClassB 22th line 'end'.
input in command window
>> obj_a=ClassA;
>> obj_b=ClassB(obj_a);
>> obj_a.y=4;
When program stop in ClassB 22th line .You check“evnt.AffectedObject”,you will find obj-a's property 'x' 'y' is stored here,not ’ obj-a‘
What is object ,include 'property' 'method' 'event'………………,not only the property.
So why are there only properties in ’evnt.AffectedObject‘, but not the full object?
classdef ClassA < handle
properties (SetObservable) %或(SetObservable=true)
x=1;
y=2;
end
events
kk
end
methods
function obj=ClassA()
x=1;
end
end
end
classdef ClassB < handle
methods
%构造函数,输入参数event_obj是产生事件的对象
function obj = ClassB(event_obj)
%为x的PreSet、为y的PostSet分别添加两个听众
if nargin > 0
addlistener(event_obj,'x','PreSet',@ClassB.eventsCBFunc);
addlistener(event_obj,'y','PostSet',@ClassB.eventsCBFunc);
end
end
end
methods (Static)
%两个听众的回调函数
function eventsCBFunc(src,evnt)
switch src.Name
case 'x'
fprintf(['Current event:' evnt.EventName]);
fprintf(1,' \n x is %s\n',num2str(evnt.AffectedObject.x))
case 'y'
fprintf(['Current event:' evnt.EventName]);
fprintf(1,' \n y is %s\n',num2str(evnt.AffectedObject.y))
end
end
end
end
0 Comments
Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks 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!