matlab 取点画图,Matlab关于figure中获取鼠标位置点画图问题

在本测试程序中,共有三个文件,untitled.fig、untitled.m和ButtonmoveFcn。

思路:在figure中的axes点击鼠标,画出该点击的位置A,然后滑动鼠标,获取滑动后鼠标的位置点B

不断画出以A、B为两顶点的矩形(清除滑动过程所画的矩形,只保留最后停留点的矩形)。

程序部分代码如下:

1、axes的ButtonDownFcn函数

function axes1_ButtonDownFcn(hObject, eventdata, handles)

global down;

global start_x;

global start_y;

%global end_x;

%global end_y;

pt = get(gca,'CurrentPoint');

start_x = pt(1,1);

start_y = pt(1,2);

hstartpoint=plot(start_x,start_y,'ro');

hold on

set(handles.text1,'string',num2str(start_x));

set(handles.t2,'string',num2str(start_y));

down=1;

set(gcf,'WindowButtonMotionFcn',@ButtonmoveFcn);

handles.hstartpoint=hstartpoint;

guidata(hObject,handles);

end

2、axes的ButtonmoveFcn函数

function ButtonmoveFcn(src,event)

global down;

global start_x;

global start_y;

if down==1

h1=findobj(gca,'tag','h1');

h2=findobj(gca,'tag','h2');

h3=findobj(gca,'tag','h3');

h4=findobj(gca,'tag','h4');

%hp=findobj(gca,'tag','hp1');

if any(h1)

delete(h1);

end

if any(h2)

delete(h2);

end

if any(h3)

delete(h3);

end

if any(h4)

delete(h4);

end

%if any(hp)

%    delete(hp);

%end

pt = get(gca,'CurrentPoint');

x = pt(1,1);

y = pt(1,2);

hp=plot(x,y,'tag','hp1','color','r','Marker','o');hold on

h1=line([start_x x],[start_y start_y],'tag','h1');

h2=line([x x],[start_y y],'tag','h2');

h3=line([x start_x],[y y],'tag','h3');

h4=line([start_x start_x],[y start_y],'tag','h4');

ht3=findobj(gcf,'tag','t3');

ht4=findobj(gcf,'tag','t4');

set(ht3,'string',num2str(x));

set(ht4,'string',num2str(y));

end

end

问题:1、在点击鼠标后,移动鼠标时,图上不断画出移动后鼠标点B;如果将axes的ButtonmoveFcn函数中的注释%去掉,出现坐标系上显示鼠标点位置与B点不符。

2、当预先在axes上画一图时,例如plot(gca,1:10)后,不能运行axes的ButtonDownFcn函数。

请大家指教啊!

99e39a8196ca8f63dc1eee7b0d72d0ed.gif

2009-7-7 23:54 上传

点击文件名下载附件

8.39 KB, 下载次数: 16289