是否可以在 WindowButtonMotionFcn 中使用多个回调?
我创建了一个类,为构造中的图形添加功能。该类为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相关文章可以在 Yair Altman 的 Undocumented MATLAB 博客上找到,来自客座博主马特·惠特克。您所指的是回调链,并引用博客中的内容:
幸运的是,该文章中似乎有一个替代解决方案。使用那里发布的代码片段,我能够获得一个在鼠标移动时执行的函数,而无需设置“WindowButtonMotionFcn”。我向当前图形添加了一个侦听器,如下所示:
当我在窗口中移动鼠标时,会显示消息
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:
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:And the message
hello
was displayed when I moved the mouse in the window.您可以通过 cellfun 和 feval 来完成此操作,如 Mathworks 网站上的回答:http: //www.mathworks.com/matlabcentral/answers/10664-multiple-callback-functions
请注意,回调被设置为使用 cellfun 评估每个的匿名函数处理程序。
You can do this via cellfun and feval, as answered on Mathworks site: http://www.mathworks.com/matlabcentral/answers/10664-multiple-callback-functions
Note that the callback is set to an anonymous function using cellfun to evaluate each handler.