What software functionality requires triggering callback functions by adding or removing dynamic attributes?
2 views (last 30 days)
Show older comments
In MATLAB, it appears that only 'dynamic properties' can trigger built-in events(PropertyAdded/PropertyRemoved) when added or removed. For example, in the following code snippet. But from a product design perspective, the necessity of using the addition/removal of 'dynamic properties' to trigger callback functions for changing text in a figure is not very significant.
So, in which software requirements is it necessary to use the addition/removal of dynamic properties to trigger callback functions?"
%main.m
function main()
clear all
clc
import MyFigure.* %Import packge
%Creat a figure object fig_obj
fig_obj=MyFigure([0.2,0.2,0.50,0.50]);
%Creat class AddDynoProFigHandlea;=AddDynoProp(fig_obj);
%pause; %暂停
%Add 'dynamic properties'Data for fig_obj through object ab
ab.AddPropForMyFigure(fig_obj,'Data');
%Use findprop to obtain the meta.DynamicProperty object corresponding to the member 'Data'
m = fig_obj.findprop('Data');
%将函数set_Data的句柄指派到SetMethod成员上
m.SetMethod = @set_Data;
%成员Data的数据设置
fig_obj.Data=peaks;
delete(ab);
end
MyFigure.m
%+MyFigure\@MyFigure\MyFigure.m
classdef MyFigure<dynamicprops
properties
FigHandle; %图形窗口句柄
AxHandle; %轴对象句柄
end
methods
function obj = MyFigure (pos) %构造函数
if nargin > 0
if length(pos) == 4
obj.FigHandle = figure('Unit','normalized','Position',pos,...
'Name','动态属性成员事件演示','NumberTitle','off',...
'MenuBar','none');
else
error('Improper position')
end
end
obj.AxHandle =axes('Parent',obj.FigHandle,...
'Unit','normalized',...
'Pos',[0.1,0.1,0.7,0.8]);
uicontrol('Parent',obj.FigHandle,...
'Unit','normalized',...
'Pos',[0.82,0.1,0.15,0.1],...
'Style','pushbutton',...
'String','退出',...
'Callback','close');
end
end
end
AddDynoProp.m
%+MyFigure\@AddDynoProp\AddDynoProp.m
classdef AddDynoProp < handle
properties
mp; %动态属性成员对应的meta.DynamicProperty对象
end
methods
%构造函数,evtobj为产生事件的对象
function obj = AddDynoProp(evtobj)
if nargin > 0
%创建两个听众
addlistener(evtobj,'PropertyAdded',@MyFigure.AddDynoProp.PropEvents);
addlistener(evtobj,'PropertyRemoved',@MyFigure.AddDynoProp.PropEvents);
end
end
%在对象evtobj上添加属性成员DynoPropName为产生事件的对象
function AddPropForMyFigure(obj,evtobj,DynoPropName)
obj.mp = evtobj.addprop(DynoPropName);
end
%析构函数
function delete(obj)
delete(obj.mp); %删除动态属性成员
end
end
methods (Static)
%回调函数
function PropEvents(src,evnt)
switch evnt.EventName
case 'PropertyAdded'
title(src.AxHandle,'PropertyAdded 事件发生了');
case 'PropertyRemoved'
title(src.AxHandle,'PropertyRemoved 事件发生了');
set(src.AxHandle,'Visible','off');
end
end
end
end
set_Data
function set_Data(obj,val)
obj.Data = val;
surf(obj.Data); %绘制Data的曲面图
end
0 Comments
Answers (0)
See Also
Categories
Find more on Package and Share Apps 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!