I have made my GUI using GUIDE. I have an axes (tag=axes1). An image is displayed in the axes. When i click on the image(inside axes) I need to get the coordinates. I get the coordinate values when image is not added to axes.But image is displayed in axes do no obtain the axes. How to get the coordinates?
Asked
Active
Viewed 3.0k times
2 Answers
24
Let's assume that you've plotted the image with handle imageHandle
that:
imageHandle = imshow(imageObj);
You should assign the ButtonDownFcn
to the image handle not the axes handle that:
set(imageHandle,'ButtonDownFcn',@ImageClickCallback);
and get the mouse coordinates from this function as follows:
function ImageClickCallback ( objectHandle , eventData )
axesHandle = get(objectHandle,'Parent');
coordinates = get(axesHandle,'CurrentPoint');
coordinates = coordinates(1,1:2);
%// then here you can use coordinates as you want ...
end
You can try this small demo to check my answer:
function demoOnImageClick
clc;clear;
imObj = rand(500,500);
figure;
hAxes = axes();
imageHandle = imshow(imObj);
set(imageHandle,'ButtonDownFcn',@ImageClickCallback);
function ImageClickCallback ( objectHandle , eventData )
axesHandle = get(objectHandle,'Parent');
coordinates = get(axesHandle,'CurrentPoint');
coordinates = coordinates(1,1:2);
message = sprintf('x: %.1f , y: %.1f',coordinates (1) ,coordinates (2));
helpdlg(message);
end
end

Sameh K. Mohamed
- 2,323
- 4
- 29
- 55
-
2@HelloASP , you can accept the answer if it satisfy your requirements of the question to end the discussion in this question. – Sameh K. Mohamed Feb 06 '13 at 15:06
-
@HelloASP please make sure to check Sameh's answers as correct. thnx :] – Michael Lorenzo Jul 08 '14 at 01:01
-
Thanks for the example. Maybe replace `imshow` by `imagesc` since not all Matlab users have the [Image Processing Toolbox](http://mathworks.com/help/images/index.html). – Svaberg Jan 31 '15 at 09:40
2
You can get the axes handle from the image handle by
ah = get( imageHandle, 'Parent' );
Then you can get the mouse click position by
p = get( ah, 'CurrentPoint' );

Shai
- 111,146
- 38
- 238
- 371