The Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   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
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.