How to get the handles of a subplot?

Dear all,
I want to have a good control to images shown in a subplot figure using a right click of the mouse.
My code as follows:
function my_test()
handles.f = figure;
handles.a = subplot(2,1,1)
imshow('cameraman.tif');
handles.b = subplot(2,1,2)
imshow('moon.tif');
%
set(handles.f,'ButtonDownFcn',{@imageX_ButtonDownFcn});
guidata(handles.a,handles);
function imageX_ButtonDownFcn(hObject, eventdata, handles)
handles=guidata(hObject);
switch lower(get(handles.f, 'selectiontype'))
case 'alt' % right click
I = gcf %get the image from this subplot where you made the right click
BW = im2bw(I) %do something
imshow(BW); %show BW in the same subplot
end
I want of I made a right click to the upper subplot then the image in this subplot will be binarized. Same thing for the lower subplot.
I think the above code is not working because I am not having access to the subplot handles. So I have two subplot.
Any idea how to make it works?
Thank you very much.
Meshoo

1 Comment

handles will not get passed to a user created callback so you would want to remove that 3rd input argument from your
function imageX_ButtonDownFcn(hObject, eventdata, handles)
Obviously you can explicitly pass it in, but you should never do this. What you are doing instead, in getting handles from the hObject is the correct way to get the handles, if you also do what Jan Simon suggests below.

Sign in to comment.

 Accepted Answer

Do not use the ButtonDownFcn of the figure, but of the subplots:
% Instead of: set(handles.f,'ButtonDownFcn',{@imageX_ButtonDownFcn});
set(handles.a, 'ButtonDownFcn', @imageX_ButtonDownFcn);
set(handles.b, 'ButtonDownFcn', @imageX_ButtonDownFcn);
Then hObject is the subplot's handle in the callback.

6 Comments

Thank you very much. I'm not sure if I understood well your suggestion but I tried the following code:
function my_test()
handles.f = figure;
handles.a = subplot(2,1,1);
imshow('cameraman.tif');
handles.b = subplot(2,1,2);
imshow('moon.tif');
%
set(handles.a, 'ButtonDownFcn', @imageX_ButtonDownFcn);
set(handles.b, 'ButtonDownFcn', @imageX_ButtonDownFcn);
guidata(handles.a,handles);
guidata(handles.b,handles);
function imageX_ButtonDownFcn(hObject, eventdata)
handles = guidata(hObject);
switch lower(get(handles, 'selectiontype'))
case 'alt' % right click
I = gcf %get the image from this subplot where you made the right click
BW = im2bw(I) %do something
imshow(BW); %show BW in the same subplot
end
I still can't make the call back if I made a right click with the mouse.
Actually, when I made my comment above to use this answer I didn't consider this closely enough. If you have an image on an axes then you cannot actually click the axes to hit its ButtonDownFcn, you need to use the image object's ButtonDownFcn instead. i.e.
handles.image(1) = imshow('cameraman.tif');
...
handles.image(2) = imshow('moon.tif');
set(handles.image(1), 'ButtonDownFcn', @imageX_ButtonDownFcn);
set(handles.image(2), 'ButtonDownFcn', @imageX_ButtonDownFcn);
To test this very quickly try the following code:
figure; h = imagesc( magic(20) );
set( gca, 'ButtonDownFcn', @(src,evt) disp( 'Axes clicked' ) )
and try clicking. Then add the following line and try clicking again:
set( h, 'ButtonDownFcn', @(src,evt) disp( 'Image clicked' ) )
If you do not need the image to catch mouse events, you can make it "transparent" for the mouse by setting its 'HitTest' to off. Then the callback of the axes is sensitive again.
True, so the choice is yours really, depending whether it is more useful to have the axes handle or the image handle directly in your callback.
In a general case it is easier to retrieve the axes handle from the image (it will always be its 'parent') whereas you may plot many objects on the axes so retrieving the image from the 'Children' array can be messy, but if you just have an axes and 1 image then either works fine.
Indeed thank you very much Adam and Jan.

Sign in to comment.

More Answers (0)

Asked:

on 11 Nov 2016

Commented:

on 16 Nov 2016

Community Treasure Hunt

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

Start Hunting!