Reading mouse position while hovering
Show older comments
Hi.
I understand this question is very similar to these:
Nevertheless, I am trying to show some information (preferably as a text object) while mouse is over a part of an image in a continuous way: meaning that the text will appear if the mouse is over specified pixels and disappear otherwise. I came across using the WindowButtonMotionEvent and listeners instead of WindowButtonMotionFcn, however it is not clear to me how exactly this can be implemented properly.
function pixHover(x,y)
Thresh = 40;
set(gcf,'WindowButtonMotionFcn', @hoverCallback);
axesHdl = axes;
function hoverCallback(src, evt)
textHdl = text('Color', 'black', 'VerticalAlign', 'Bottom');
mousePoint = get(axesHdl, 'CurrentPoint');
mouseX = mousePoint(1,1);
mouseY = mousePoint(1,2);
distancesToMouse = hypot(x - mouseX, y - mouseY);
[~, ind] = min(abs(distancesToMouse));
xrange = range(get(axesHdl, 'Xlim'));
yrange = range(get(axesHdl, 'Ylim'));
if abs(mouseX - x(ind)) < Thresh && abs(mouseY - y(ind)) < Thresh
set(textHdl, 'String', {['x = ', num2str(x(ind))], ['y = ', num2str(y(ind))]});
set(textHdl, 'Position', [x(ind) + 0.01*xrange, y(ind) + 0.01*yrange])
else
set(textHdl, 'String', '')
delete(textHdl)
end
end
end
The problem is that after finding the target point (x,y), and displaying the text object it never enter the function again (and therefore, the text persists and cannot be deleted after leaving the target point). I appreciate any help in advance.
Accepted Answer
More Answers (0)
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!