问题
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).
I've found that
impixelinfoval
does exactly this but I'm not able to extract the pixel coordinates from thisuicontrol
in the script (are they stored in some field?).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?
回答1:
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.
来源:https://stackoverflow.com/questions/24797795/matlab-get-mouse-coordinates-on-image-without-clicking-on-mouse-over