Is it possible to use multiple callbacks in the WindowButtonMotionFcn?

callback, event-handling, matlab, user-interface

Solution

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.

Problem

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.

Original source