필터 지우기
필터 지우기

How to get the exact position of a plotted point?

조회 수: 60 (최근 30일)
BF83
BF83 2011년 8월 17일
댓글: maziar 2021년 5월 15일
Hi,
I've plotted a graph and additionally marked a specific point. A 'ButtonDownFcn' is assigned to the point, to enable user interaction.
By clicking onto the point I would like to receive its exact x and y coordinates.
My approach:
function getPlotPointCoord()
clc;
figure(1);
x = -2*pi:0.05:+2*pi;
subplot(1,2,1);
hold on;
plot(x, sin(x));
% plot point at (x=0, y=0)
plot(0, sin(0), 'bx', 'ButtonDownFcn', @getPoint);
function getPoint(varargin)
currentPoint = get(gca, 'CurrentPoint');
fprintf('Hit Point! Coordinates: %f, %f \n', ...
currentPoint(1), currentPoint(3));
end
end
The call _get(gca, 'CurrentPoint') for instance returns the following values:
Hit Point! Coordinates: 0.000000, 0.002924
Hit Point! Coordinates: 0.000000, 0.002924
Hit Point! Coordinates: 0.000000, 0.002924
Hit Point! Coordinates: -0.106952, -0.008772
Hit Point! Coordinates: 0.106952, -0.014620
Is there a way to receive the true values (x=0, y=0) of the 'button' instead of the current (most likely) mouse-coordinates?
Thank you!

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 17일
What do you mean? You want the x/y value of the data points? You can get that from menu Tools->Data Cursor and then click along the curve.
  댓글 수: 8
Trishank Sharma
Trishank Sharma 2018년 7월 8일
편집: Trishank Sharma 2018년 7월 8일
Hi what if I have a set of 60 points (n=60), where x and y are real and imaginary parts of a complex number Z. Can I know the point number (the 'n'th point) beside the coordinates or do I have to do it manually?
maziar
maziar 2021년 5월 15일
Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

BF83
BF83 2011년 8월 18일
A working example for completion:
function getPlotPointCoord()
clc;
figure(1);
x = -2*pi:0.05:+2*pi;
subplot(1,2,1);
hold on;
plot(x, sin(x));
plot(0, sin(0), 'bx', 'ButtonDownFcn', {@getPoint, 0, sin(0)});
function getPoint(src, eventdata, x, y)
currentPoint = get(gca, 'CurrentPoint');
fprintf('Hit Point! Mouse Coordinates: %f, %f \n', ...
currentPoint(1), currentPoint(3));
fprintf('Hit Point! Real Coordinates: %f, %f \n', x, y);
end
end

Tarami Readus
Tarami Readus 2019년 1월 23일
Where in the code does he add the marker, and to what specific point? I would like to know how to do that

Adam Albayati
Adam Albayati 2019년 5월 8일
function getPlotPointCoord()
clc;
figure(1);
x = -2*pi:0.05:+2*pi;
subplot(1,2,1);
hold on;
plot(x, sin(x));
% plot point at (x=0, y=0)
plot(0, sin(0), 'bx', 'ButtonDownFcn', @getPoint);
function getPoint(varargin)
currentPoint = get(gca, 'CurrentPoint');
fprintf('Hit Point! Coordinates: %f, %f \n', ...
currentPoint(1), currentPoint(3));
end
end

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by