MATLAB: How to get the handles of a subplot

handles of a subplot figureImage Processing ToolboxMATLAB

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

Best 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.