Matlab:将全局坐标转换为图形坐标 [英] Matlab: convert global coordinates to figure coordinates

查看:586
本文介绍了Matlab:将全局坐标转换为图形坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我通过

coords = get(0,'PointerLocation');

如何将它们转换为通过ginput获得的点?

How can I convert them into points gotten via ginput?

即,我想从

coords = get(0,'PointerLocation');
coords=someConversion(coords);

就像我打电话一样

coords=ginput(1);

然后在代码的前一部分中,在与鼠标相同的位置单击图形.

And clicking inside the figure in the same spot as the mouse was in the previous bit of code.

推荐答案

以下是如何进行此转换的示例...

Here's an example of how you can do this conversion...

假设您有一个图形,并且该图形包含带有手柄hAxes的轴对象.使用功能 ginput 可以选择轴.要从get(0, 'PointerLocation')获取一组等效的点(提供与屏幕有关的坐标),必须考虑图形位置,轴位置,轴宽度/高度和轴限制.

Let's say you have a figure, and that figure contains an axes object with handle hAxes. Using the function ginput would allow you to select points within the axes. To get an equivalent set of points from get(0, 'PointerLocation'), which gives coordinates in relation to the screen, you have to account for the figure position, axes position, axes width/height, and axes limits.

执行此操作比较棘手,因为您希望将位置度量单位设置为相同单位.如果要以像素为单位计算所有内容,这意味着必须将对象的'Units'属性设置为'pixels',获取位置,然后将'Units'属性设置回以前的状态.我通常使用自己的函数get_in_units来完成这一部分:

Doing this is tricky because you want to have the position measures in the same units. If you want to compute everything in units of pixels, this means you'd have to set the 'Units' properties of the objects to 'pixels', get the positions, then set the 'Units' properties back to what they previously were. I usually make my own function get_in_units to do this part:

function value = get_in_units(hObject, propName, unitType)

  oldUnits = get(hObject, 'Units');  % Get the current units for hObject
  set(hObject, 'Units', unitType);   % Set the units to unitType
  value = get(hObject, propName);    % Get the propName property of hObject
  set(hObject, 'Units', oldUnits);   % Restore the previous units

end

使用上述功能,您可以创建另一个功能get_coords,该功能获取屏幕坐标并将其转换为轴坐标:

Using the above function, you can make another function get_coords which gets the screen coordinates and converts them to axes coordinates:

function coords = get_coords(hAxes)

  % Get the screen coordinates:
  coords = get_in_units(0, 'PointerLocation', 'pixels');

  % Get the figure position, axes position, and axes limits:
  hFigure = get(hAxes, 'Parent');
  figurePos = get_in_units(hFigure, 'Position', 'pixels');
  axesPos = get_in_units(hAxes, 'Position', 'pixels');
  axesLimits = [get(hAxes, 'XLim').' get(hAxes, 'YLim').'];

  % Compute an offset and scaling for coords:
  offset = figurePos(1:2)+axesPos(1:2);
  axesScale = diff(axesLimits)./axesPos(3:4);

  % Apply the offsets and scaling:
  coords = (coords-offset).*axesScale+axesLimits(1, :);

end

生成的coords应该与使用 ginput .请注意,如果axes对象嵌套在任何 uipanel对象中,图,您还必须考虑面板位置.

The resulting coords should be close to those you would get from using ginput. Note that if the axes object is nested within any uipanel objects in the figure, you will have to account for the panel positions as well.


为说明以上代码的行为,这是一个简洁的小示例.创建上述功能后,请创建第三个功能:

To illustrate the behavior of the above code, here's a neat little example. After creating the above functions, create this third function:

function axes_coord_motion_fcn(src, event, hAxes)

  coords = get_coords(hAxes);               % Get the axes coordinates
  plot(hAxes, coords(1), coords(2), 'r*');  % Plot a red asterisk

end

然后运行以下代码:

hFigure = figure;  % Create a figure window
hAxes = axes;      % Create an axes in that figure
axis([0 1 0 1]);   % Fix the axes limits to span from 0 to 1 for x and y
hold on;           % Add new plots to the existing axes
set(hFigure, 'WindowButtonMotionFcn', ...  % Set the WindowButtonMotionFcn so
    {@axes_coord_motion_fcn, hAxes});      %   that the given function is called
                                           %   for every mouse movement

当您将鼠标指针移到图形轴上时,应该看到在其后部绘制了红色星号的痕迹,如下所示:

And when you move the mouse pointer over the figure axes, you should see a trail of red asterisks being plotted behind it, like so:

这篇关于Matlab:将全局坐标转换为图形坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆