MATLAB: How to disable the WindowButtonDownFcn callback when panning/zooming in App Designer

buttondownfcnclicksdisableMATLABpanselectionchangedfcnwindowbuttondownfcnzoom

How do I disable my WindowButtonDownFcn callback when panning/zooming in App Designer?

Best Answer

For ordinary figures, ButtonDown callbacks are disabled when zoom/pan are selected. However, this is not the case for UIFigures in App Designer, which is why you are running into an issue.
One way to restrict how often the button click callback is triggered is to restrain the clicks to just the line plot itself. You can do this by setting the "ButtonDownFcn" of your plot object when you first create it:
Try creating a plot in the app's StartupFcn, then defining a ButtonDown callback as a nested function within that same StartupFcn. With the ButtonDownFcn callback, clicks will only be recorded if the user touches the plot line itself.
Next, we need to disable the ButtonDownFcn callback any time an AxesToolbar button (like pan or zoom) is turned on.
To detect whether a toolbar button has been selected, you can use the "SelectionChangedFcn" callback for an AxesToolbar object:
This callback will be triggered any time you click zoom/pan. Within this callback you can check two things:
1) If the selected button is "on", then delete your plot object's ButtonDownFcn callback. This will prevent clicks on the line from being recorded.
2) If the selected button is "off", restore the plot object's ButtonDownFcn callback, so that clicks are once again recorded.
I have implemented all this in the StartupFcn of the attached MLAPP file. I have also included output statements to note when a click is being registered and when a toolbar button is being selected/deselected.
For convenience, I am also pasting the code from my StartupFcn below:
function startupFcn(app)
% plot some data
x = 1:5; y = 1:5;
p = plot(app.UIAxes,x,y,'ButtonDownFcn',@line_click);
% set up callback for toolbar
app.UIAxes.Toolbar = axtoolbar(app.UIAxes,{'zoomin','zoomout','pan','datacursor'},...
'SelectionChangedFcn',{@tbar_change,p});
% define callbacks as nested functions
function line_click(~,~)
disp('click!');
end
function tbar_change(~,event,p)
state = event.Selection.Value; % ToolBarStateButton has a "Value of "on" or "off"
% if selected, turn off line_click callback
if state == 'on'
disp('toolbar button selected; callback turned off');
p.ButtonDownFcn = [];
% if not selected, turn back on line_click
else
disp('toolbar button de-selected; callback turned back on');
p.ButtonDownFcn = @line_click;
end
end
end