如何在Matlab图形窗口中找到插入线端点的坐标

问题描述 投票:1回答:1

我在Matlab图形窗口中用Insert画了一条线。如何找到终点的坐标?

enter image description here

matlab matlab-figure
1个回答
0
投票

在您的情况下,主要问题是绘制的点和使用Insert绘制的线的坐标相对于不同的原点和轴。

您的散点图具有所示轴的坐标(白色矩形),而线具有整个图形窗口的坐标(灰色矩形)。有关MATLAB如何组织此过程的更多详细信息,请参见here

要在轴坐标系中获取直线端点的坐标,必须将坐标转换为该坐标系。

 % Random scatter data plotted for example
 x = rand(10,1); y = rand(10,1); scatter(x,y)

 % Retrieve position values of axes box (in figure coordinates)
 ax_pos = get(gca, 'Position');
 ax_pos_offset_x = ax_pos(1);
 ax_pos_offset_y = ax_pos(2);
 ax_pos_width = ax_pos(3);
 ax_pos_height = ax_pos(4);

 % Retrieve position of line (in figure coordinates, the line needs to be marked in the figure window to allow inspecting it with `gco`
 x_line_pos = get(gco, 'X');
 y_line_pos = get(gco, 'Y');

 % Transform coordinates to axes coordinate system
 x_prime = (x_line_pos - ax_pos_offset_x) * diff(xlim) / ax_pos_width;
 y_prime = (y_line_pos - ax_pos_offset_y) * diff(ylim) / ax_pos_height;

给予

>> x_prime
x_prime =
    0.3392    0.6548

>> y_prime
y_prime =
    0.6132    0.1865

与图中线的位置相匹配

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.