2

I've been googling for a while to find a solution to this apparently simple problem: getting mouse coordinates in image axes while the mouse is moving (without clicking).

  1. I've found that impixelinfoval does exactly this but I'm not able to extract the pixel coordinates from this uicontrol in the script (are they stored in some field?).

  2. I've also found that ginput gives you the coordinates but only if you click.

Do you know any workaround for one of these two issues?

Are you aware of any solution to accomplish this using MATLAB functions?

Amro
  • 123,847
  • 25
  • 243
  • 454
Giulia
  • 95
  • 11
  • 4
    you need to define a call back and query the `'CurrentPoint'` property. See [this answer](http://www.mathworks.com/matlabcentral/answers/97563-how-do-i-continuously-read-the-mouse-position-as-the-mouse-is-moving-without-a-click-event). – Shai Jul 17 '14 at 07:56
  • 1
    It works, thank you very much. Indeed I found that also getting the field 'String' from the uicontrol impixelinfoval gives you the coordinates (although you need to extract them from the string). – Giulia Jul 17 '14 at 08:40
  • 1
    feel free to post a full answer to your question according to your finding, so future SO users will also benefit from what you've just learned. – Shai Jul 17 '14 at 08:49
  • A more generally robust way I feel is to use the WindowButtonMotionFcn callback within the figure, then there is no need to extract anything from strings or any other craziness :) - I added a full answer below – Shaun314 Jul 17 '14 at 16:08

1 Answers1

0

Check out this callback for the figure window: WindowButtonMotionFcn - http://www.mathworks.com/help/matlab/ref/figure_props.html (you do have to scroll down a decent amount, sorry about that)

This function will fire every time the mouse moves anywhere within the figure. From there, you need to make a call to get the 'CurrentPoint' of the axis you want. If the currentpoint is within the axis bounds, then you know that your mouse is over the axis and currentpoint tells you, well the current point :)

If you are not in the axis, your ButtonMotionFcn will quickly exist and so performance won't be affected noticeably.

The nice thing about this approach is that you can use it for any axes all within the same function.

Shaun314
  • 3,191
  • 4
  • 22
  • 27