Asked by Amanda
on 7 Feb 2013

This is where we are getting stuck. We would appreciate any insight.

We have a basic figure (linear plot using: and we are using ‘CurrentPoint’ in the code to extract a value from the graph.

Here is the input:

function FcnName(src,evnt,a)

cp = get(gcf, 'CurrentPoint')

disp('click down!!!!')

disp(a)

end

Here is the output:

cp = 341 257

but what we need is the "real" x and y value. Is there a way to accomplish this?

Amanda

Answer by Azzi Abdelmalek
on 7 Feb 2013

Accepted Answer

x=get(gco,'Xdata');

y=get(gco,'Ydata')

Azzi Abdelmalek
on 8 Feb 2013

get(gca,'Xdata')

Error: The name 'Xdata' is not an accessible property for an instance of class 'axes'.

to get all axes properties, Type

set(gca)

To get just the current point coordinate, there is a function ginput

Azzi Abdelmalek
on 8 Feb 2013

Image Analyst
on 8 Feb 2013

Sign in to comment.

Answer by Jan
on 8 Feb 2013

Edited by Jan
on 4 Nov 2019

I guess that you do not want the coordinates relative to the figure, but relative to the data in an AXES object. Then:

CP = get(gca, 'CurrentPoint');

x = CP(1);

y = CP(2);

The CurrentPoint property of the axes replies a 2x3 array, which defines the viewing line through a 3D scene. But in standard 2D view, the CP(1, 1:2) contain the current 2D position already. This property is the "location of last button click, in axes data units", see Doc: axes_props.

Choszit Lee
on 17 Jul 2018

Thank you so much, Jan. You saved my life...orz

Sign in to comment.

Answer by Image Analyst
on 8 Feb 2013

Edited by Image Analyst
on 8 Feb 2013

Amanda, give this a try. It will plot some data points, then ask you to click near one, and it will tell you which point of your data that you clicked closest to.

% Plot data - a line from (1,1) to (10,10).

h=plot(1:10, 'bs-')

grid on;

axis equal;

xlim([0 11]);

ylim([0 11]);

datacursormode on;

% Enlarge figure to full screen.

screenSize = get(0,'ScreenSize')

set(gcf, 'units','pixels','outerposition', screenSize);

% Ask user to click on a point.

uiwait(msgbox('Click near any data point'));

% Print the x,y coordinates - will be in plot coordinates

[x,y] = ginput(1) % Will be close to 5,5 but not exactly.

% Mark where they clicked with a cross.

hold on;

plot(x,y, 'r+', 'MarkerSize', 20, 'LineWidth', 3);

% Print the coordinate, but this time in figure space.

% Coordinates will be way different, like 267, 196 instead of 5,5.

cpFigure = get(gcf, 'CurrentPoint')

cpAxis = get(gca, 'CurrentPoint')

% Print coordinates on the plot.

label = sprintf('(%.1f, %.1f) = (%.1f, %.1f) in figure space', x, y, cpFigure(1), cpFigure(2));

text(x+.2, y, label);

% Tell use what ginput, cpFigure, and cpAxis are.

message = sprintf('ginput = (%.3f, %.3f)\nCP Axis = [%.3f, %.3f\n %.3f, %.3f]\nCP Figure = (%.3f, %.3f)\n',...

x, y, cpAxis(1,1), cpAxis(1,2), cpAxis(2,1), cpAxis(2,2), cpFigure(1), cpFigure(2));

uiwait(msgbox(message));

% Retrieve the x and y data from the plot

xdata = get(h, 'xdata')

ydata = get(h, 'ydata')

% Scan the actual ploted points, figuring out which one comes closest to 5,5

distances = sqrt((x-xdata).^2+(y-ydata).^2)

[minValue minIndex] = min(distances)

% Print the distances next to each data point

for k = 1 : length(xdata)

label = sprintf('D = %.2f', distances(k));

text(xdata(k)+.2, ydata(k), label, 'FontSize', 14);

end

% Draw a line from her point to the closest point.

plot([x xdata(minIndex)], [y, ydata(minIndex)], 'r-');

% Tell her what data point she clicked closest to

message = sprintf('You clicked closest to point (%d, %d)',...

xdata(minIndex), ydata(minIndex));

helpdlg(message);

Sean de Wolski
on 8 Feb 2013

Make sure you have a bunch of points that are whatever in 'x' but not in y

x = rand(1,500)*10;

y = rand(1,500)*10000;

Now click near a point that has a second point at about the same y but much different x.

I'll see if I can find my code.

Image Analyst
on 8 Feb 2013

Baha
on 13 Nov 2014

Dear Image Analyst,

This was actually a great tutorial for what I was thinking if it was possible to do in matlab. Is it possible to extend this code for clicking in 3D and finding the closest data point? I appreciate your guidance...

Baha

Sign in to comment.

Answer by Simão Faria
on 11 Oct 2016

I think you just have to change gcf to gca.

The coordinates you are getting are relative to the whole figure object and not the current axes.

If you use:

cp = get(gca, 'CurrentPoint')

you should get the coordinates according to the X and Y axis assigned to the plot area

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Jan (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/62653-convert-currentpoint-to-x-and-y-value#comment_128010

Sign in to comment.