MATLAB Answers

App designer: how to get mouse position when I click on figure?

432 views (last 30 days)
Maxime Hervo
Maxime Hervo on 19 Jun 2018
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

  4 Comments

Show 1 older comment
Maxime Hervo
Maxime Hervo on 21 Jan 2019
Here is the aswer from Mathworks:
"I understand that you are inquiring about the missing 'CurrentPoint' property for UIAxes in MATLAB R2017b.
I agree that this is a limitation to the UIAxes object, and would create an enhancement request on your behalf so that my colleagues in the development team can consider adding this feature in future releases of MATLAB.
In the meantime, there is currently a wokaround using the 'IntersectionPoint' property:
>> ax = uiaxes;
>> s = scatter3(ax, randn(100,1),randn(100,1),randn(100,1));
>> s.ButtonDownFcn = @(~,e) disp(e.IntersectionPoint);
However, the workaround above would only work in MATLAB R2018a."
It should now be fixed in R2019a, pre-release.
Fei Gao
Fei Gao on 22 Jan 2019
I fixed this problem using the method you mentioned above in R2018b. Thank you very much!
Derek Burrage
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!).

Sign in to comment.

Answers (2)

Siwan Yang
Siwan Yang on 22 Jun 2018
Hello Maxime,
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

  0 Comments

Sign in to comment.


Charles Pruszynski
Charles Pruszynski on 24 Dec 2019
Edited: Charles Pruszynski on 2 May 2020
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
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.

Sign in to comment.

Products


Release

R2018a