How to get the exact position of a plotted point?

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!

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 17 de Ag. de 2011

2 votos

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 comentarios

BF83
BF83 el 17 de Ag. de 2011
Thanks for your answer.
I don't want to use the data cursor in this special case. I wonder if there is a easy way to obtain these coordinates just by using the 'ButtonDownFcn' assigned to the specific point.
Fangjun Jiang
Fangjun Jiang el 17 de Ag. de 2011
Okay, then I guess you could write a few more lines to compare the current point to see which data point is the closest.
Fangjun Jiang
Fangjun Jiang el 17 de Ag. de 2011
Wait a minute! Can't you do this?!
plot(0, sin(0), 'bx', 'ButtonDownFcn', 'disp([0,sin(0)])');
BF83
BF83 el 17 de Ag. de 2011
Ok! Perhaps I can pass the coordinates to a function, for instance to @getPoint?
Fangjun Jiang
Fangjun Jiang el 17 de Ag. de 2011
Yes. Like:
plot(0, sin(0), 'bx', 'ButtonDownFcn', 'getPoint(0,sin(0))')
BF83
BF83 el 17 de Ag. de 2011
Okay, great. I'm sure that it will solve my problem. I'm gonna test it tomorrow.
Thank you for coming up with this easy and elegant solution.
Trishank Sharma
Trishank Sharma el 8 de Jul. de 2018
Editada: Trishank Sharma el 8 de Jul. de 2018
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 el 15 de Mayo de 2021
Thanks

Iniciar sesión para comentar.

Más respuestas (3)

BF83
BF83 el 18 de Ag. de 2011
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 el 23 de En. de 2019

0 votos

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 el 8 de Mayo de 2019

0 votos

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

Etiquetas

Preguntada:

el 17 de Ag. de 2011

Comentada:

el 15 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by