MATLAB Answers

Find the coordinates of a point chosen on a plot

208 ビュー (過去 30 日間)
Nom 2019 年 11 月 12 日
コメント済み: Nom 2019 年 11 月 15 日
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:

  0 件のコメント



darova 2019 年 11 月 14 日
Short example
x = linspace(0,2,20)'; % generate some data
y = sin(x);
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

  1 件のコメント

Nom 2019 年 11 月 15 日
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


その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2019 年 11 月 12 日
Use data cursor, set "SnapToDataVertex" to be "on" and then get the position. See example in
doc datacursormode

  1 件のコメント

Nom 2019 年 11 月 13 日
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?





Community Treasure Hunt

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

Start Hunting!

Translated by