Handle.listener, how to update code?

4 次查看(过去 30 天)
Kaden
Kaden 2018-10-8
Hi, I have an old MathWorks code that I did not write and contains handle.listener in it. It appears that Matlab has phased out this function and was replaced by addlistener. In the code below, I am getting an error in the line with the handle.listener function, how can I update the code so it continues to work as before??
Thank you!!
function updateAncestorListeners(h_group,update_fcn)
%updateAncestorListeners Set up listeners to ancestors.
% updateAncestorListeners(H_GROUP,UPDATE_FCN) sets up listeners to all
% ancestors of H_GROUP that have Position, XLim, or YLim properties. H_GROUP
% is an hggroup object. UPDATE_FCN is a function handle that is called when
% any of the ancestor properties change.
%
% It's important to use updateAncestorListeners when you are drawing objects
% in a way that may depend on the current Position, XLim, or YLim properties
% of ancestors. If these properties change, you need to redraw your
% object. Using updateAncestorListeners makes the code drawing objects more
% robust to user actions like zooming and resizing.
% $Revision $ $Date: 2005/05/27 14:07:37 $
% Clear the old listeners.
setappdata(h_group, 'AncestorPositionListeners', []);
h_parent = get(h_group, 'Parent');
root = 0;
listeners = [];
property_list = {'Position', 'XLim', 'YLim'};
while h_parent ~= root
% Some ancestor objects might not have Position properties.
properties = get(h_parent);
for k = 1:numel(property_list)
property = property_list{k};
if isfield(properties, property)
parent_handle = handle(h_parent);
listener = handle.listener(parent_handle, ...
parent_handle.findprop(property), ...
'PropertyPostSet', update_fcn);
if isempty(listeners)
listeners = listener;
else
listeners(end + 1) = listener;
end
end
end
h_parent = get(h_parent, 'Parent');
end
setappdata(h_group, 'AncestorPositionListeners', listeners);
Copyright 2005 The MathWorks, Inc.
  3 个评论
Kaden
Kaden 2018-10-9
Thanks! Unfortunately I saw this earlier and wasn't able to get my code running with it. I believe my error is with "update_fcn" . Any ideas on how this input needs to be modified to go from a handle.listener to a addlistener accepted input? I didn't fully understand the fourth argument difference.
Thanks!
Kevin Chng
Kevin Chng 2018-10-9
Hi Kaden, do you mind provide h_group and update_fcn script/data file for me try out?

请先登录,再进行评论。

回答(1 个)

Divyajyoti Nayak
Divyajyoti Nayak 2024-12-26
Hi Kaden,
To update code from using ‘handle.listener’ to ‘addlistener’, the ‘eventName’ or the third argument needs to be changed. Here’s the documentation of ‘addlistener’ which describes the requirements of the input arguments:
Hence, the new code would be something like:
listener = addlistener(parent_handle, parent_handle.findprop(property), 'PostSet', update_fcn);
It is important to note that the parent class whose property is being listened to must set the the ‘GetObservable’ and ‘SetObservable’ attribute. Only then, listening to these properties is possible.

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by