Find the coordinates of a point chosen on a plot

Hello,
So I've been using ginput to select points on a plot
My plot consists of time(x) and velocity(y).
Below is my user-selected points.
[Userx,Usery] = ginput(1);
However, I noticed it doesn't actually select points from the voltage vs. time graph and instead just a point in the figure screen.
How can I get the nearest data point of the actual plot?
I've found one solution is to use this user created script file:
https://www.mathworks.com/matlabcentral/fileexchange/3265-magnetginput

1 Kommentar

Can anyone help me, what is the problem of this error - Undefined function or variable 'pdist2'. . Do i want to make the 'pdis2' function ?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

darova
darova am 14 Nov. 2019
Short example
x = linspace(0,2,20)'; % generate some data
y = sin(x);
plot(x,y,'.-b')
xy = ginput(1); % get point
[~,ix] = min(pdist2([x y],xy)); % find closest point
hold on
plot(x(ix),y(ix),'or') % display closest point
hold off

3 Kommentare

Awesome I pretty much used a very simple formula which does the same thing you have listed above.
[x, ~] = ginput(1); %xmouse, ymouse
[~, xidx] = min(abs(time-x)); %closest index
xx = time(xidx); %extract
Can anyone help me, what is the problem of this error - Undefined function or variable 'pdist2'. . Do i want to make the 'pdis2' function ?
pdist2() comes from Statistics and Machine Learning Toolbox.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Fangjun Jiang
Fangjun Jiang am 12 Nov. 2019

0 Stimmen

Use data cursor, set "SnapToDataVertex" to be "on" and then get the position. See example in
doc datacursormode

1 Kommentar

Nom
Nom am 13 Nov. 2019
Bearbeitet: Nom am 13 Nov. 2019
Is there a way to do this using ginput, at least where I can see the x-line and y-line of the crosshair which chooses the point?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Exploration finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2017b

Gefragt:

Nom
am 12 Nov. 2019

Kommentiert:

am 9 Mai 2023

Community Treasure Hunt

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

Start Hunting!

Translated by