以下是如何进行此转换的示例…
假设你有一个数字,那个数字包含一个带有句柄hAxes的轴对象.使用功能ginput可以选择轴内的点.要从get(0,’PointerLocation’)获得一组等效的点,它提供与屏幕相关的坐标,您必须考虑图形位置,轴位置,轴宽度/高度和轴限制.
这样做很棘手,因为您希望以相同的单位进行位置测量.如果要以像素为单位计算所有内容,这意味着您必须将对象的“单位”属性设置为“像素”,获取位置,然后将“单位”属性设置回原先的位置.我通常使用自己的函数get_in_units来完成这一部分:
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来获取屏幕坐标并将它们转换为轴坐标:
function coords = get_coords(hAxes)
% Get the screen coordinates:
coords = get_in_units(0, 'PointerLocation', 'pixels');
% Get the figure posi