在Matlab中,使用鼠标在GUI上绘图

6

我希望能在Matlab中编写一个带有GUI的程序,在运行程序时,用户可以使用鼠标在GUI中的坐标轴上绘制任何内容,同时我想将创建的图像保存在矩阵中。请问如何实现?

3个回答

8

最终我找到了一段好的代码,并对其中的一些部分进行了定制化。通过这种方式,用户可以使用鼠标在坐标轴上绘制任何东西:

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','')

2
我如何使用这些函数来绘图? - Mike Glaz

3

我正在开发一个程序来检测用户所绘制的字符,因此用户应该能够绘制字母数字字符。 - hamed aj

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接