首页 > 编程知识 正文

matlab坐标转换命令,matlab改变坐标轴位置

时间:2023-05-06 06:31:35 阅读:10690 作者:1005

以下是此转换的示例

假设有一个数字包含带有手柄hAxes的轴对象。 使用功能ginput可以选择轴内的点。 若要从get(0,’pointer location’)获取一组等效点,必须考虑图元位置、轴位置、轴宽度/高度和轴限制,以提供与屏幕相关的坐标。

这很难。 因为我想用同样的单位测量位置。 要计算所有内容(以像素为单位),必须将对象的“单位”属性设置为“像素”,获取位置,然后将“单位”属性返回到其原始位置。 通常,这部分使用自己的函数get_in_units完成。

function value=get _ in _ units (h object,propName,unitType ) )

OLDUnits=get(hobject,' units '; % Get the current units for hObject

set(hobject,' Units ',unitType ); % Set the units to unitType

value=get (h对象,propName; % getthepropnamepropertyofhobject

set(hobject,' Units ',oldUnits ); % Restore the previous units

结束

使用上述函数,可以创建另一个函数get_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 ' ).' ];

% computeanoffsetandscalingforcoords :

offset=figurepos(1:2 ) axespos ) 1:2;

axesscale=diff(axeslimits )./axespos ) 3:4;

% applytheoffsetsandscaling :

COORDS=(COORDS-offset ).*axesscaleaxeslimits(1, );

结束

生成的坐标应该接近使用ginput获得的坐标。 请注意,如果轴对象嵌套在图中的ui面板对象中,则还必须考虑面板的位置。

示例:

为了说明上述代码的行为,这里有一个简单的小例子.创建上述函数后,创建第三个函数:

function axes _ coord _ motion _ fcn (src,event,hAxes ) ) )。

CORDS=get_COORDS(Haxes ); % Get the axes coordinates

plot(Haxes,Coords(1),Coords(1),' r* ' ); % Plot a red asterisk

结束

执行以下代码:

hFigure=figure; % Create a figure window

hAxes=axes; % Create an axes in that figure

axis([0101]; % fixtheaxeslimitstospanfrom0to1for xandy

保持接通; % 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

将鼠标指针移动到图表轴上时,后面会出现红色星号,如下所示:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。