Using WindowButtonDownFcn in App Designer

조회 수: 57 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2019년 4월 17일
편집: Will Grant 2020년 5월 28일
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?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2019년 4월 24일

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
          plot(app.UIAxes2,app.Var1)
      end
  end

NOTE: The CurrentPoint property of UIAxes was introduced in R2019a

추가 답변 (1개)

Will Grant
Will Grant 2020년 5월 28일
편집: Will Grant 2020년 5월 28일
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));
OR
p = plot([1 2], [1 2]);
p.ButtonDownFcn = @(o, e) clickHandler(o, e);
function clickHandler(o, e)
...
end

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by