App designer: how to get mouse position when I click on figure?
Show older comments
Good afternoon,
I am converting Programmatic GUI to app designer. I want to get the mouse position when a user click on a figure.
Unfortunately the CurrentPoint property is not available on the UIaxes. is there a work around?
(Since 2017a we can use the buttonBown property but I do not see the point if we do not know where the user clicked buttonDown )
Thanks a lot, Maxime
7 Comments
Fei Gao
on 20 Jan 2019
Have you fixed this problem? I get in trouble with the same problem.
Maxime Hervo
on 21 Jan 2019
Fei Gao
on 22 Jan 2019
I fixed this problem using the method you mentioned above in R2018b. Thank you very much!
Derek Burrage
on 4 Sep 2019
I am new to appdesigner, but have the same problem (in MATLAB 2018a): I want to extract the mouse click coords and display them in a text box (say) in the GUI. However, I found the code hard to understand and implement (lot's of errors, no results!)
I am not sure if this should be a comment or posted as a separate question, but I put it here because it is closely tied to the suggested code above.
Taking the suggested code line by line.
1) ax-uiaxes => Why not app.UIAxes?
2) s=scatter 3... I understand this is a standard MATLAB plot function, which, presumably works inside an app., and its handle is s.
3) What does this syntax mean? My interpretation (possibly in error) is the following:
@ signifies the handle to an anonymous inline function, but what is argument 'e', Is it another handle? Is it assigned to the display function?
I would expect disp( ) to return the button press coords (in the MATLAB workspace?), but somehow it should be available inside the app
What class or function does the property IntersectionPoint belong to?
Finally, on the left side: Is s.ButtonDownFcn a handle, or a property of s and how can I use it to put the results (button press coords) in a text box using, e.g. the following code in a relevant call back: app.TextArea.Value=s.ButtonDownFcn(1,1:2); %'5'; or some variation (none of which seem to work!).
Wonsup Lee
on 17 Jul 2020
I got success by referring to this answer: https://kr.mathworks.com/matlabcentral/answers/506032-how-do-i-select-a-point-on-matlab-uiaxes-and-then-get-data-for-it
Just use drawpoint function instead of drawrectangle.
One line in callback function or a main code.
[x, y] = selectDataPoints(app, app.figArea);
And, create a function.
function [x, y] = selectDataPoints(~, ax)
roi = drawpoint(ax);
x = roi.Position(1);
y = roi.Position(2);
end
Eric Sargent
on 9 Dec 2020
(Full answer below)
Paul Ben Ishai
on 4 Jan 2021
You sir, are a genius! It works like a treat!
Accepted Answer
More Answers (2)
Siwan Yang
on 22 Jun 2018
1 vote
Hello Maxime,
There is a similar question on MATLAB Answer: https://www.mathworks.com/matlabcentral/answers/265820-saving-mouse-click-coordinates-on-image-in-gui
You could use the ‘ImageClickCallback’ function mentioned in the article to get the mouse click coordinate. You could also use ‘ginput’ function to adjust the number of coordinates you want to get.
Thanks,
Siwan
Charles Pruszynski
on 24 Dec 2019
Edited: Walter Roberson
on 8 Jun 2021
The ROI selection functions of the Image Processing Toolbox can do this in App Designer:
roiPOC = drawpoint(app.UIAxes); %Use Mouse To Select a point ROI
PosPOC=round(get(roiPOC,'Position')); %Extract Coordinates of the point ROI
delete(roiPOC); %Delete the point ROI Marker
drawnow; % Force figure update
CJP
2 Comments
Andrew Diamond
on 2 May 2020
I used this to good effect. Thanks
I'm sure you already know this, but you have roiPOA vs roiPOC typos
For posterity: odd notes for drawpoint on uiaxes vs axes
1) The cursor doesn't change to a cross hair (or the like)
2) If you don't delete the point obect (i.e. the deletion of the returned point object), as shown, and then you right click for the context menu, it throws an error. So its deletion is a practical necessity.
Charles Pruszynski
on 2 May 2020
Thanks. Fixed the typos.
CJP
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!