是否可以在 WindowButtonMotionFcn 中使用多个回调?

发布于 2024-11-09 05:05:52 字数 476 浏览 0 评论 0原文

我创建了一个类,为构造中的图形添加功能。该类为 WindowMouseMotion 事件创建一个侦听器;但是,要触发此事件,我必须为图窗的 WindowButtonMotionFcn 属性添加一个虚拟回调函数。我首先检查该属性是否已填充。如果不是,那么我将其设置为一个不执行任何操作的虚拟回调函数。

我可以简单地将这个虚拟回调添加到任何现有回调中,而不是检查该属性是否已设置吗?一个回调属性可以调用多个函数吗?

编辑

当使用handle.listener方法处理下面给出的WindowButtonMotionEvent事件时,请务必使用eventdata.CurrentPoint 访问当前鼠标位置。在以这种方式处理 WindowButtonMotionEvent 事件之前,图窗的 CurrentPoint 属性不会更新。

I created a class which adds functionality to a figure on construction. This class creates a listener for the WindowMouseMotion event; however, to get this event to fire I had to add a dummy callback function for the figure's WindowButtonMotionFcn property. I first check if this property is already populated. If it isn't then I set it to a dummy callback function that does nothing.

Instead of checking if the property is already set or not, can I simply add this dummy callback to any existing callbacks? Is it possible for a callback property to call multiple functions?

EDIT

When using the handle.listener approach to handle the WindowButtonMotionEvent event given below, be sure to use eventdata.CurrentPoint to access the current mouse position. The CurrentPoint property of the figure does not get updated before handling the WindowButtonMotionEvent event in this manner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗化ㄋ丶相逢 2024-11-16 05:05:52

相关文章可以在 Yair Altman 的 Undocumented MATLAB 博客上找到,来自客座博主马特·惠特克。您所指的是回调链,并引用博客中的内容:

说实话,写过代码
之前处理回调
连锁,我宁愿戳自己
用叉子戳眼睛!

幸运的是,该文章中似乎有一个替代解决方案。使用那里发布的代码片段,我能够获得一个在鼠标移动时执行的函数,而无需设置“WindowButtonMotionFcn”。我向当前图形添加了一个侦听器,如下所示:

myListener = handle.listener(gcf,'WindowButtonMotionEvent',...
                             @(hSource,eventData) disp('hello'));

当我在窗口中移动鼠标时,会显示消息 hello

A related article can be found on Yair Altman's Undocumented MATLAB blog, from guest blogger Matt Whitaker. What you are alluding to is callback chaining, and quoting from the blog:

Frankly, having written code
previously that handles callback
chaining, I would rather poke myself
in the eye with a fork!

Luckily, there appears to be an alternative solution in that article. Using a snippet from the code posted there, I was able to get a function to execute on mouse movement without having to set the 'WindowButtonMotionFcn'. I added a listener to the current figure like so:

myListener = handle.listener(gcf,'WindowButtonMotionEvent',...
                             @(hSource,eventData) disp('hello'));

And the message hello was displayed when I moved the mouse in the window.

迷你仙 2024-11-16 05:05:52

您可以通过 cellfun 和 feval 来完成此操作,如 Mathworks 网站上的回答:http: //www.mathworks.com/matlabcentral/answers/10664-multiple-callback-functions

obj = uicontrol(...,'style','popupmenu',...
  'Callback', @(h,e)(cellfun(@(x)feval(x,h,e), ...
   {@(h,e)this.myfunc(h), ...
   @(h,e)this.myfunc2(h), ...
   @(h,e)this.myfunc2(h)}))

请注意,回调被设置为使用 cellfun 评估每个的匿名函数处理程序。

You can do this via cellfun and feval, as answered on Mathworks site: http://www.mathworks.com/matlabcentral/answers/10664-multiple-callback-functions

obj = uicontrol(...,'style','popupmenu',...
  'Callback', @(h,e)(cellfun(@(x)feval(x,h,e), ...
   {@(h,e)this.myfunc(h), ...
   @(h,e)this.myfunc2(h), ...
   @(h,e)this.myfunc2(h)}))

Note that the callback is set to an anonymous function using cellfun to evaluate each handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文