Convert CurrentPoint to X and Y Value.

85 次查看(过去 30 天)
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
  1 个评论
Jan
Jan 2013-2-8
What are "real x and y values"? Realtive to the AXES, in screen coordinates or in the real world?

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-2-7
x=get(gco,'Xdata');
y=get(gco,'Ydata')
  6 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2013-2-8
编辑:Azzi Abdelmalek 2013-2-8
If you select the axes, gco do the same thing then gca, if you select a figure, gco is the same then gcf
Image Analyst
Image Analyst 2013-2-8
I guess I'm still not seeing it. Let's say a user plotted, say, 1:10, and then the user clicked in the middle of the plot. The user wanted to retrieve the coordinate (5,5) but instead got some kind of pixel coordinates like 341,257 (like the poster said). I'm just not seeing how your code turns the 341,257 into 5,5. But whatever - I don't have the code like Amanda does and she said it works for her so that's what counts.

请先登录,再进行评论。

更多回答(3 个)

Jan
Jan 2013-2-8
编辑:Jan 2019-11-4
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.

Image Analyst
Image Analyst 2013-2-8
编辑:Image Analyst 2013-2-8
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);
  7 个评论
Image Analyst
Image Analyst 2013-2-8
OK I see what you mean. My code does calculate the closest point numerically but it may not APPEAR to be the closest point on the screen because it's so compressed vertically. So if you click on a point in the middle of the screen, the closest point may be only 0.5 away but that appears way on the other side of the plot, while one that appears right next to the point, but may actually be 50 units away, and that is probably the one the user wanted to specify. It's closer on the screen but farther away numerically.
Baha
Baha 2014-11-13
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

请先登录,再进行评论。


Simão Faria
Simão Faria 2016-10-11
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
  1 个评论
Ronald Ouwerkerk
Ronald Ouwerkerk 2021-4-6
Since v2019a the datatips have been very intrusive. This feature snaps to the plot line and a small gray dot indicates the closest point on the line. If I hold the cursor, a datatip pops up with the X and Y values of the graph. It should be possible to retrieve these values. Idealy we shuld be able to suppress the datatip and keep the snap function and use the 'snapped' X and Y values in a function. All this would require getting hold of the handle of the datatip object. However, if I leave the graph and go to the command window (datatip still visible) I cannot find the datatip with
datatiphandle = findobj( gca, 'Type', 'datatip')
or with ch = get( gca, 'Children')
I can find datatip handles with
datatiphandle = findall(gcf,'Type','hggroup')
But these handles don't have the X and Y values reaily availabe and the property list does not look like the properies of a datatip generated by the function datatip. For one: the 'type' attribute is different.
How can we grab a datatip that we can see and is often annoyingly and persistently displayed over a line graph and either interogate it for X and Y line coorodinates or make it invisible?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by