Using WindowButtonDownFcn in App Designer

57 visualizaciones (últimos 30 días)
MathWorks Support Team
MathWorks Support Team el 17 de Abr. de 2019
Editada: Will Grant el 28 de Mayo de 2020
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?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 24 de Abr. de 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
          plot(app.UIAxes2,app.Var1)
      end
  end

NOTE: The CurrentPoint property of UIAxes was introduced in R2019a

Más respuestas (1)

Will Grant
Will Grant el 28 de Mayo de 2020
Editada: Will Grant el 28 de Mayo de 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));
OR
p = plot([1 2], [1 2]);
p.ButtonDownFcn = @(o, e) clickHandler(o, e);
function clickHandler(o, e)
...
end

Categorías

Más información sobre Develop uifigure-Based Apps en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by