且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Matlab的GUI上用鼠标绘图

更新时间:2023-11-24 08:53:16

最后,我找到了一个不错的代码,并且我更改了一些用于自定义的部分.通过这种方式,用户可以使用鼠标在轴上绘制任何内容:

Finally i find a good code and i have changed some parts for customizing for me. with this way, user can drawing anythings in the axes with mouse :

function userDraw(handles)
%F=figure;
%setptr(F,'eraser'); %a custom cursor just for fun

A=handles.axesUserDraw; % axesUserDraw is tag of my axes
set(A,'buttondownfcn',@start_pencil)

function start_pencil(src,eventdata)
coords=get(src,'currentpoint'); %since this is the axes callback, src=gca
x=coords(1,1,1);
y=coords(1,2,1);

r=line(x, y, 'color', [0 .5 1], 'LineWidth', 2, 'hittest', 'off'); %turning     hittset off allows you to draw new lines that start on top of an existing line.
set(gcf,'windowbuttonmotionfcn',{@continue_pencil,r})
set(gcf,'windowbuttonupfcn',@done_pencil)

function continue_pencil(src,eventdata,r)
%Note: src is now the figure handle, not the axes, so we need to use gca.
coords=get(gca,'currentpoint'); %this updates every time i move the mouse
x=coords(1,1,1);
y=coords(1,2,1);
%get the line's existing coordinates and append the new ones.
lastx=get(r,'xdata');  
lasty=get(r,'ydata');
newx=[lastx x];
newy=[lasty y];
set(r,'xdata',newx,'ydata',newy);

function done_pencil(src,evendata)
%all this funciton does is turn the motion function off 
set(gcf,'windowbuttonmotionfcn','')
set(gcf,'windowbuttonupfcn','')