MATLAB Answers

get on click coordinates from a plot (multiple times)

416 views (last 30 days)
Max Müller
Max Müller on 10 Sep 2014
Commented: Max Müller on 11 Sep 2014
the function [x,y]= ginput (n) gives back the x and y data of the curse position while clicking. The Problem: I want to give the user the possibility to click multiple times in a row. However, I need the click to have a direct effect.
So if n>1 the function waits for n clicks to be done before returning the x/y Data.I want to click --> get Values --> click --> get values --> click on the button --> finish. My idea was to use a Pushbutton which sets the ginput into an eternal loop, which can be broken be clicking agian.
function CollectePointsButton_Callback(hObject, eventdata, handles)
Counter = getappdata(hObject,'Counter');
if isempty(Counter) == 1;
Counter = 0;
Data = findobj('type','axes');
Counter = Counter +1;
function ClickToCollecte (gcbo,evendata,handles)
Data = findobj('tag','CollectePointsButton');
Counter = getappdata(Data,'Counter')
while mod(Counter,2) == 1
Data = findobj('tag','CollectePointsButton');
Counter = getappdata(Data,'Counter')
[x,y] = ginput(1);


Sign in to comment.

Accepted Answer

Mischa Kim
Mischa Kim on 10 Sep 2014
Hello Max, check out this answer.
It uses left-mouse clicks to collect data and a right-mouse click to finish collecting data.


Max Müller
Max Müller on 11 Sep 2014
this Code is really good. I guess i can us it this way:
button = 1;
while sum(button) <=1 % read ginputs until a mouse right-button occurs
[x,y,button] = ginput(1)
Can u pls explain me why it works ? So i can use it more often,
Mischa Kim
Mischa Kim on 11 Sep 2014
ginput reads position values as long as you keep using the left-mouse button ( button = 1 ). As soon as you use the right-mouse button, button = 3, and the loop is exited.

Sign in to comment.

More Answers (0)