Check if mouse is above an axes
조회 수: 14 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Joseph Cheng
2015년 5월 28일
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
Joseph Cheng
2015년 5월 28일
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.
추가 답변 (1개)
Jan
2015년 5월 28일
overobj helps to check if the mouse is on top of an axes. See http://undocumentedmatlab.com/blog/undocumented-mouse-pointer-functions
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!