Check if mouse is above an axes

21 visualizaciones (últimos 30 días)
Justin Solomon
Justin Solomon el 27 de Mayo de 2015
Comentada: Walter Roberson el 26 de Jun. de 2020
Is there a quick way to check if the mouse is currently positioned over an axes? I'm working on a GUI that sets the 'WindowScrollWheelFcn' property of the figure. However, I only want the callback function to do something if the mouse is currently over a given axes. I've been using the 'CurrentPoint' property of the figure, and then using the position property of the axes to check if the mouse is over the axes. This works fine when the axes' parent is the figure. However, for my GUI, its possible that the axes is contained in several hierarchies of uipanels, thus the axes' position property is relative to its parent panel, not the figure. This makes figuring out if the mouse is over the axes more complicated. I need this to work, independent if the axes is contained in the figure, or in a uipanel that is a descendent of the figure. Any clever ideas out there? Thanks in advance for any tips.

Respuesta aceptada

Joseph Cheng
Joseph Cheng el 28 de Mayo de 2015
Here is a little thing i threw together.
function test()
h = figure(1);
hp = uipanel('Title','Main Panel','FontSize',12,...
'BackgroundColor','white',...
'Position',[.1 .1 .9 .9]);
hsp = uipanel('Parent',hp,'Title','Subpanel','FontSize',12,...
'Position',[.1 .1 .9 .9]);
hax = axes('Parent',hsp,'position',[.1 .1 .8 .8]);
plot(randi(10,4,2));
set(h,'windowbuttonmotionfcn',@mousemove);
function mousemove(hobject,eventdata)
C = get(gca,'currentpoint');
P = get(gca,'position');
xlim = get(gca,'xlim');
ylim = get(gca,'ylim');
outX = ~any(diff([xlim(1) C(1,1) xlim(2)])<0);
outY = ~any(diff([ylim(1) C(1,2) ylim(2)])<0);
if outX&outY
set(gca,'color',[0 1 0])
else
set(gca,'color',[1 0 0])
end
title(gca, {['(X,Y) = (', num2str(C(1,1)), ', ',num2str(C(1,2)), ')'];...
[num2str([outX outY])]})
now obviously you'll have to adapt and see if it is what you're looking to do. I didn't have time to add some robustness checks for multiple axes but this mayget you where you need to go. So instead of looking at whether the mouse is within the axes position, i chose to check to see if the point is within the x and y limits of the axes.
  2 comentarios
Joseph Cheng
Joseph Cheng el 28 de Mayo de 2015
oh and i changed the logic checks so the outX and outY actually should be inX and inY. Just didn't get around to changing it when i put the ~ infront of the any.
Justin Solomon
Justin Solomon el 28 de Mayo de 2015
Thanks for the example. I recall that I previously had some trouble with using the 'currentpoint' property of the axes. I think this came about when I had multiple axes in the figure. Anyways, it seems to be working now so I'll deal with those problems if they arise again.

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 28 de Mayo de 2015
overobj helps to check if the mouse is on top of an axes. See http://undocumentedmatlab.com/blog/undocumented-mouse-pointer-functions
  2 comentarios
Justin Solomon
Justin Solomon el 28 de Mayo de 2015
Thanks for the tip. Unfortunately, overobj can only search for the immediate child of the figure. Thus if my axes is embedded in a uipanel, overobj won't find it. :(

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by