Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
933 views
in Technique[技术] by (71.8m points)

user interface - MATLAB how to get mouse click coordinates

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...