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

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

Have you fixed this problem? I get in trouble with the same problem.
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.
I fixed this problem using the method you mentioned above in R2018b. Thank you very much!
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!).
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
As of R2020b you can use the ginput function to get the coordinates of a selected point in an axes.
(Full answer below)
You sir, are a genius! It works like a treat!

Sign in to comment.

 Accepted Answer

As of R2019a, the CurrentPoint property is available in uiaxes.
As of R2020b you can use the ginput function to get the coordinates of a selected point in an axes.
It requires a specific setup as noted in the documentation (see link below):
The ginput and gtext functions do not have an argument for specifying the target figure. As a result, you must set the HandleVisibility property of the App Designer figure to 'callback' or 'on' before calling these functions. After you call these functions, you can set the HandleVisibility property back to 'off'. For example, this code shows how to define a callback that allows you to identify the coordinates of two points using the ginput function.
function pushButtonCallback(app,event)
app.UIFigure.HandleVisibility = 'callback';
ginput(2)
app.UIFigure.HandleVisibility = 'off';
end

More Answers (2)

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
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

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.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 19 Jun 2018

Edited:

on 16 May 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!