Why does the callback function create an 'meta.property 'object

2 views (last 30 days)
When you wanna instantiate an object. First you need creat a class or you already have a class. Then you use a class to instantiate an object.
Or you already object, then you copy a object .
But look this code. I make a breakingpoint in 15th line of ClassB 'switch src.Name',then excute:
>> obj_a=ClassA;
>> obj_b=ClassB(obj_a);
>> obj_a.x=3;
When the program runs to 15th line of ClassB.m . I found 'src' is a 'meta.property 'object! Why does the callback function create an 'meta.property 'object? How does the callback function create an 'meta.property 'object?
I check the addlistener ,this function allow to input 'meta.property 'object for 'PropertyName' ,but it dose not creat a 'meta.property 'object.
Obviously 'x' as the input arguments of the addlistener function(7th line of ClassB.m) is the character, not the 'meta.property' object.
ClassA.m
classdef ClassA < handle
properties (SetObservable) %或(SetObservable=true)
x=1;
y=2;
end
end
ClassB.m
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

Accepted Answer

fa wu
fa wu on 14 Aug 2023
I got the answer
the documents Listen for Changes to Property ValuesDefine Callback Function for Property Event
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

More Answers (0)

Categories

Find more on Events in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!