Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: how to check if the mouse is inside a subplot or not?
Replies: 2   Last Post: Oct 31, 2015 5:12 PM

 Messages: [ Previous | Next ]
 Ki Posts: 21 Registered: 5/20/12
how to check if the mouse is inside a subplot or not?
Posted: Oct 28, 2015 9:48 PM

I have two subplots at top and bottom of a figure. I capture the mouse motion and would like to tell if the current mouse position is inside the top subplot or the bottom subplot or somewhere else. I search the newsgroup and found someone is using WindowButtonMotionFcn, and i found the following code

set(gcf, 'WindowButtonMotionFcn', @mmCB);

function mmCB(src, ev)
curPos = get(gca, 'CurrentPoint');
para = get(gcf, 'UserData');

topAxPos = get(para{1}, 'position'); % para{1} is the handle for the top subplot
botAxPos = get(para{2}, 'position'); % para{2} is the handle for the bottom subplot
if ( (curPos(1)>topAxPos(1)) && (curPos(2)>topAxPos(2)) && (curPos(1)<(topAxPos(1)+topAxPos(3))) && (curPos(2)<(topAxPos(2)+topAxPos(4))) ) % top subplot
disp('top');
elseif ( (curPos(1)>botAxPos(1)) && (curPos(2)>botAxPos(2)) && (curPos(1)<(botAxPos(1)+botAxPos(3))) && (curPos(2)<(botAxPos(2)+botAxPos(4))) ) % bottom subplot
disp('bottom')
else
disp('no in the subplot')
end
end

But it didn't give me the expect result. I think get(gca, 'position') give the position the subplot in the figure but currentpoint give the inside coordinate of the axes, they are not in the same coordinate. Any idea how to make it work? Thanks.

Date Subject Author
10/28/15 Ki
10/31/15 dpb
10/31/15 dpb