AppDesigner: Getting a UIAxes' enableDefa​ultInterac​tivity to work with a UIFigure's WindowButtonDownFcn?

6 views (last 30 days)
Dear all,
I would greatly appreciate any help with the following problem I am encountering. I will try to describe everything as clear as possible.
My Goal:
I am trying to create an app in AppDesigner where I can 'select' points in a figure. The selection will have to work similar to a 'brush', where all points are triggered in proximity of the mouse. The behaviour looks similar to the defualt brush function, but that function only allows dragging of a box. The data I use in my figures is more curved and complex, and would not suit the 'square' approach of the brush function very well.
I want the figure to work with the "enableDefaultInteractivity" behaviour of MATLAB UIAxes. This means that i can drag the figure by clicking and holding the mouse, and zoom with the mousewheel on the location the pointer is at that point. When clicking a button, I want to switch to a "brushing-mode", where i can click in the figure and start "painting" datapoints in my figure.
The Problem:
Whenever the WindowButtonDownFcn of the app itself is added, this will automatically disable the enableDefaultInteractivity of the figure. By default, I would right-click the "app.UIFigure" in the Component Browser, and select "Callbacks -> Window Callbacks -> add WindowButtonDownFcn callback", but this immediately disables the enableDefaultInteractivity and I cannot restore it.
As a workaround, I defined the desired WindowButtonDownFcn in the apps startupFcn as follows:
app.UIFigure.WindowButtonDownFcn = {@MouseDown, app};
This at least means I have expected behaviour up to the point of performing the desired tasks. However, when the datapoint selection is completed (through a WindowButtonMotionFcn and a WindowButtonUpFcn), the enableDefaultInteractivity is disabled, and I cannot restore it.
The Code:
I have appended a dumbed-down app that demonstrates the problem. It is a very simple app containing one single figure and one button. Upon running the code, a square is plotted. The figure can be dragged and zoomed as expected. When the button is pressed, the user can click (and hold down!) the mousebutton in the figure, and drag the displayed 'brush' over the datapoints, which will then light up.
When releasing the button, the points remain visible. However, now the figure cannot be dragged or zoomed anymore.
Any help is appreciated! Kind regards,

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 8 Jul 2022
There seems to be a subtle bug going on with your app. I can narrow down the issue to this code:
% Code in your startupFcn
f = figure;
ax = axes;
f.WindowButtonDownFcn = @(~,~) disp('');
% At this point in the script, the act of setting WindowButtonDownFcn
% *should* have disabled the default interactivity, but due to a bug it
% remains active.
% Code in ButtonPushed
% At this point in the script, enableDefaultInteractivity didn't work
% because WindowButtonDownFcn is not empty, so interactions remain
% disabled.
Fortunately, there is a really easy fix to this issue: Move most of the code in your startupFcn into ButtonPushed.
  1. Don't set WindowButtonDownFcn or WindowButtonMotionFcn or WindowButtonUpFcn within startupFcn.
  2. Instead, set all three Fcn properties within your ButtonPushed callback (before calling uiwait), and then clear all three property values after uiwait returns.
Mark Kamps
Mark Kamps on 11 Jul 2022
I got my function definition wrong, apparently I have to call them as @app.MouseUp, rather than @MouseUp. Now it works as expected.
I can confirm that the enableDefaultInteractivity is defined immediately when the mouse button is released. I tried it on a multitude of locations and/or with duplicates of the enableDefaultInteractivity command.
I found that defining "axes(app.Figure)" will immediately enable the enableDefaultInteractivity, although this does leave a noticable 'flicker' on my axes. I am not sure if this is how it shoudl work?
In any way, thank you for the assistance!

Sign in to comment.

More Answers (0)


Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!