MATLAB: How to conditionally execute ‘WindowButtonMotionFcn’, depending on the active icon in toolbar

MATLAB

I have some MATLAB code that generates plots. When I use the callback function 'WindowButtonMotionFcn', i want it to detect the active button in toolbar (ZoomIn/ZoomOut etc…) and accordingly be able to conditionally execute the call backfunction 'WindowButtonMotionFcn'. How do I do it ?

Best Answer

This can be achived in two alternative methods:
I) In the first method, get the handle to the Toolbar buttons as children of 'FigureToolBar', which in turn is a child of Figure object. The steps are as below:
1. Write a function "myCallback.m" (attached), that executes the callback only when a specific button is pressed.
2. Associate the callback function for 'WindowButtonMotionFcn' to 'myCallback' above, as below:
set(gcf,'WindowButtonMotionFcn',@myCallback)
II) If the objective is to conditionally execute ''WindowButtonMotionFcn', just based on Zoom button press in the Figure Toolbar, it can be done, by utilizing the ZOOM function, as follows:
1. Write a function "myCallback2.m" (attached)
2. Associate the callback function for 'WindowButtonMotionFcn' to 'myCallback2', as below:
set(gcf,'WindowButtonMotionFcn',@myCallback2)