How to get WindowButtonMotionFcn to track mouse position

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

Here's the question that'll help you, there are more two relevant answers to it. Link

7 Comments

I've already tried to use both the code proposed by Adam and Ameer wich are more suitable for my purpose but the code gives me this error " Error: Function definition not supported in this context. Create functions in code file." about the "function mouseMove(~, ~, ax, gobj)". In addiction Ammer suggests tu use the windowbuttonmotionfcn callback but I'm not able to find it on the GUI.
Basically if I try to create a callback related to the axes handle the only callback available is the ButtonDownFcn one and no others.
WindowButtonMotionFcn is supported by figure, not the axes object.
Got it thank you. Is there a way to track mouse's position in axes object?
There's a property of axes called CurrentPoint. Read Adam's answer more carefully, that should be the way to do what you want to achieve.
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
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 Graphics Objects 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!