MATLAB: Reading mouse position while hovering

windowbuttonmotionfcn

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.
I adapted the code from here :
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.

Best Answer

Ive - I think part of the problem is that you are creating a new textHdl object each time the hoverCallback fires....which will be every time that you move the mouse. So you will never be able to hide the original text object that you created, since you have created another again and again.
This variable needs only to be defined once outside of the callback. You can then adjust the string contents as you move the mouse around the axes. For example,
function pixHover(x,y)
Thresh = 40;
set(gcf,'WindowButtonMotionFcn', @hoverCallback);
axesHdl = axes;
textHdl = text('Color', 'black', 'VerticalAlign', 'Bottom');
function hoverCallback(src, evt)
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', '')
end
end
end
Since hoverCallback is nested within the pixHover (parent) function, then it has access to the local variables defined there. This includes the handle to the text object, textHdl. As the mouse moves, if we find that the threshold is met for one of the (x,y) pairs, then we just update the String and Position for textHdl. Else, we just set the string to be empty which "hides" it from us. (I suppose we could just manipulate the Visible property instead to explicitly hide the text object.)
Try the above and see what happens!