Reputation: 2225
I am using a while loop and within that I add ginput in MATLAB to capture the positions of mouse. I check every time if the returned position is within some area so I will plot some curve on the current figure. But the problem is, by using ginput, I have to press enter before the positions are returned. Is that any way to capture the mouse event such that when the current cursor hover over some points, a callback function will be triggered? Thanks.
Upvotes: 2
Views: 1458
Reputation: 627
Since you already have a figure you're using, you could set the listening property for the figure:
set(gcf,'WindowButtonMotionFcn', @mouseMoveListener);
But now you have to create a function called 'mouseMoveListener' (if you want to name it something else, change the words after the @ sign to whatever name you want, and make sure the actual event function is named that too).
Within your function mouseMoveListener
you can now get the mouse coordinates:
MousePos = get(mainAxis,'CurrentPoint');
Which tells the current point of the mouse with respect to the axes coordinates. From there, you can have whatever if statement check that the position is where you want it and perform whatever tasks you want based on that information.
Upvotes: 3