MATLAB: How to display an image in a separate figure when I click the mouse in MATLAB 7.10 (R2010a)

buttondownfcnMATLABsubplot

I want to display twelve images simultaneously, prompt a user to select an image by clicking on it with a mouse, save the selected image, and then display the image in a separate figure for further manipulation.

Best Answer

You can use SUBPLOT to display multiple images on a single figure.
The mouse click event can be handled by using the BUTTONDOWNFCN property of the respective axes objects.
For example, see code below:
function mouse_click
f = figure
%plotting 1st image
subplot(1,2,1);
img1 = imread('peppers.png');
axis1 =image(img1);
%plotting 2nd image
subplot(1,2,2);
img2 = imread('fabric.png');
axis2 =image(img2);
%setting ButtonDownFcn callbacks
set(axis1,'ButtonDownFcn',@(h,e)on_click(h,e));
set(axis2,'ButtonDownFcn',@(h,e)on_click(h,e));
end
%callback function
function on_click(hObject,eventdata)
data = get(hObject,'CData');
f1 = figure;
imshow(data)
end
The sample files 'peppers.png' and 'fabric.png' are attached below. They are also available as part of the Image Processing Toolbox.