How to get WindowButtonMotionFcn to track mouse position

56 views (last 30 days)
Hello everyone,
I'm writing a GUI code in wich I basically need to track the position of the mouse in a UIAxes ,and then draw a veritcal line in this position, where some plot are already existing. I've seen in other questions that a WindowButtonMotionFcn could help me to do that but I'm not able to create it. I've also tried with a get(axes_handle,'CurrentPoint') but the problem comes out when I need to point right over a prevoius line for wich no output is given by the "get" function. Could anyone explain me how to get a WindowButtonMotionFcn or at least give me suggestion on how to track mouse position?
Thank you in advance for your help.

Accepted Answer

Mario Malic
Mario Malic on 10 Jan 2021
Here's the question that'll help you, there are more two relevant answers to it. Link
  7 Comments
NICOLA REVELIN
NICOLA REVELIN on 10 Jan 2021
Thank you for the help, I've already tried to run and understand that script but the problem is on this line:
set(ax(1).Parent,'windowbuttonmotionfcn', {@mouseMove, ax, gobj});
where the errore says
Error while evaluating Figure WindowButtonMotionFcn.
In a nutshell I cannot set any WindowButtonMotionFcn because when I try to set a callback on the app.Axes the only one available is the ButtonDownFcn
Naum Derzhi
Naum Derzhi on 9 Jun 2021
WIndowButtonMotion event is generated by the figure, and you need to attach your callback to the figure. You can determine to which figure your axes belongs, checking the object hierarchy. Keep in mind, that axes's parent may be something other than the figure, and you need to move up the object hierarchy until to get to the figure parent (where Type is 'Figure'). In the callback attached to this event you can interrogate all axes children of this window to find out within which axes the cursor was at the time of the event (compare the axes.CurrentPoint to axes limits). Keep in mind, if there are things other than axis in the window, it may happen that the cursor was outside all of them at the time of the event.

Sign in to comment.

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!