Reputation: 392
I draw a line with Insert
in a Matlab figure window. How to find the coordinates of the end points?
Upvotes: 1
Views: 318
Reputation: 3711
The principal problem in your case is that the points your plot and the line you draw with Insert
have their coordinates relative to different origins and axes.
Your scatter plot has the coordinates of the shown axes (the white rectangle), while the line has coordinates of the full figure window (gray rectangle). See here for more details on how MATLAB is organizing this.
To get the coordinates of the endpoints of the line in the axes coordinate system, you have to transform the coordinates into that coordinate system.
% 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;
gives
>> x_prime
x_prime =
0.3392 0.6548
>> y_prime
y_prime =
0.6132 0.1865
which matches the position of the line in the plot
Upvotes: 2