Using WindowButtonDownFcn in App Designer

146 views (last 30 days)
I'm trying to access data from a line in a UIAxes in my app I'm building in App Designer, but I'm unable to set a ButtonDownFcn for a UIAxes.
How can I access the x-coordinate of a point in a line so that I can generate a new plot whose x-limits are within a specified range of that x-value?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Apr 2019

While ButtonDownFcns are not supported for UIAxes, UIFigures do support WindowButtonDownFcns. Additionally, selecting UIAxes that are children of said UIFigure, as well as selecting children of that UIAxes will trigger the WindowButtonDownFcn for the UIFigure.

If you would like the WindowButtonDownFcn to only be triggered when a point in your line is selected, you can use a combination of an if statement and tags to only trigger the callback when the line is selected.

First when plotting your original line, you should add a tag that can be referenced later:

plot(app.UIAxes, app.Var1, 'Tag', 'allData');

Next, create a WindowButtonDownFcn for your UIFigure by right clicking on your UIFigure in the Component Browser, selecting "Callbacks" from the context menu, then selecting "WindowButtonDownFcn". Once this callback is created, you can use something similar to below to check if there is a CurrentObject selected (i.e. a line was selected and not a blank space in the either the UIFigure or UIAxes), and if the tag of that CurrentObject matches the tag of your line. Beginning in MATLAB R2019a, you can then use the CurrentPoint property of UIAxes to determine where within the UIAxes was selected. All together this would look like as follows:

function UIFigureWindowButtonDown(app, event)
    %Create if statement that determines if the user clicked on the
    %line of the top UIAxes. If they didn't, do nothing
    if ~isempty(event.Source.CurrentObject) && isequal(event.Source.CurrentObject.Tag,'allData')
          %Store the CurrentPiont of the UIAxes as app.a
          app.a = app.UIAxes.CurrentPoint(1);
          alpha = 200;
          %Set the XLim of the bottom UIAxes to be +/- alpha of the
          %current point
          app.UIAxes2.XLim = [(app.a-alpha) (app.a+alpha)];
          %Plot the data on the bottom UIAxes

NOTE: The CurrentPoint property of UIAxes was introduced in R2019a

More Answers (1)

Will Grant
Will Grant on 28 May 2020
Edited: Will Grant on 28 May 2020
The modern way of handling this behavior is to set an event handler on the line/surf/etc plot objects themselves.
This gets around messing with limits, testing which object fired, etc.
plot([1 2], [1 2], 'ButtonDownFcn', @(o, e) clickHandler(o, e));
p = plot([1 2], [1 2]);
p.ButtonDownFcn = @(o, e) clickHandler(o, e);
function clickHandler(o, e)


Find more on Develop uifigure-Based Apps 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!