How to get the subplot number by clicking on it?

23 views (last 30 days)
Is there a way to get the subplot number by clicking on the individual subplot by using the mouse?
I know that there are ways to click on the subplot and make it pop out of the original subplot figure; but is there a way to simply get the subplot number for the subplot that was clicked?
  4 Comments
dpb
dpb on 27 Jan 2015
Edited: dpb on 27 Jan 2015
I "don't do windows" so not very versed in GUIs and all, and you don't give any clues as to how you're doing the plotting and selecting but gca returns the axes handle after the selection (click). Setting an axes callback function to return that should work.
Comparing the handle to the position in the saved array of handles (you did save the handles on creating, didn't you--Ok, I knew you did :) ) will return the number. Or, you can save an identifier in the 'userdata' field for the purpose. I don't know if there's a more direct way, sorry...
Sara
Sara on 27 Jan 2015
Can't you add the number of the subplot as the subplot title?

Sign in to comment.

Accepted Answer

Chad Greene
Chad Greene on 27 Jan 2015
Edited: Chad Greene on 27 Jan 2015
If you can add a tag when creating the data like this
for k = 1:4
subplot(2,2,k)
set(gca,'tag',num2str(k))
end
Then you could write a simple function using waitforbuttonpress like
function clicksubplot
while 1 == 1
w = waitforbuttonpress;
switch w
case 1 % keyboard
key = get(gcf,'currentcharacter');
if key==27 % (the Esc key)
try; delete(h); end
break
end
case 0 % mouse click
mousept = get(gca,'currentPoint');
x = mousept(1,1);
y = mousept(1,2);
try; delete(h); end
h = text(x,y,get(gca,'tag'),'vert','middle','horiz','center');
end
end
  3 Comments

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!