poster
2019-12-10, 20:41
如果我通过获得坐标
coords = get(0,'PointerLocation'); 如何将它们转换为通过ginput获得的点?
即,我想从中获得相同的值
coords = get(0,'PointerLocation'); coords=someConversion(coords); 就像我打来的电话一样
coords=ginput(1); 然后在上一行代码中,在与鼠标相同的位置单击图形内部。
回答:
这是一个如何进行此转换的示例...
假设您有一个图形,并且该图形包含带有句柄hAxes的axes对象。使用功能ginput (https://www.mathworks.com/help/matlab/ref/ginput.html)将允许您选择轴内的点。要从get get(0, 'PointerLocation')获得等效点集,该点集给出相对于屏幕的坐标,您必须考虑图形位置,轴位置,轴宽度/高度和轴限制。
这样做很棘手,因为您希望以同一单位使用位置测量。如果要以像素为单位计算所有内容,这意味着必须将对象的'Units'属性设置为'pixels' ,获取位置,然后将'Units'属性设置回原来的状态。我通常使用自己的函数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 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 (https://www.mathworks.com/help/matlab/ref/ginput.html)会得到的ginput (https://www.mathworks.com/help/matlab/ref/ginput.html) 。请注意,如果轴对象嵌套在图中的任何uipanel对象 (https://www.mathworks.com/help/matlab/ref/uipanel.html)中,则还必须考虑面板的位置。
例:
为了说明以上代码的行为,这是一个简洁的小例子。创建上述功能后,请创建此第三个功能:
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 并且,当您将鼠标指针移到图形轴上时,应该看到在其后面绘制了红色星号的轨迹,如下所示:
https://i.stack.imgur.com/ufC6S.jpg (https://i.stack.imgur.com/ufC6S.jpg)
更多&回答... (https://stackoverflow.com/questions/2769430)
coords = get(0,'PointerLocation'); 如何将它们转换为通过ginput获得的点?
即,我想从中获得相同的值
coords = get(0,'PointerLocation'); coords=someConversion(coords); 就像我打来的电话一样
coords=ginput(1); 然后在上一行代码中,在与鼠标相同的位置单击图形内部。
回答:
这是一个如何进行此转换的示例...
假设您有一个图形,并且该图形包含带有句柄hAxes的axes对象。使用功能ginput (https://www.mathworks.com/help/matlab/ref/ginput.html)将允许您选择轴内的点。要从get get(0, 'PointerLocation')获得等效点集,该点集给出相对于屏幕的坐标,您必须考虑图形位置,轴位置,轴宽度/高度和轴限制。
这样做很棘手,因为您希望以同一单位使用位置测量。如果要以像素为单位计算所有内容,这意味着必须将对象的'Units'属性设置为'pixels' ,获取位置,然后将'Units'属性设置回原来的状态。我通常使用自己的函数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 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 (https://www.mathworks.com/help/matlab/ref/ginput.html)会得到的ginput (https://www.mathworks.com/help/matlab/ref/ginput.html) 。请注意,如果轴对象嵌套在图中的任何uipanel对象 (https://www.mathworks.com/help/matlab/ref/uipanel.html)中,则还必须考虑面板的位置。
例:
为了说明以上代码的行为,这是一个简洁的小例子。创建上述功能后,请创建此第三个功能:
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 并且,当您将鼠标指针移到图形轴上时,应该看到在其后面绘制了红色星号的轨迹,如下所示:
https://i.stack.imgur.com/ufC6S.jpg (https://i.stack.imgur.com/ufC6S.jpg)
更多&回答... (https://stackoverflow.com/questions/2769430)