在matlab中使用鼠标在GUI上绘图

时间:2012-09-21 18:42:59

标签: matlab matlab-guide

我想在matlab中使用GUI编程,在运行程序时,用户可以在GUI中的轴上用鼠标绘制任何东西,我想将创建的图像保存在矩阵中。我怎么能这样做?

3 个答案:

答案 0 :(得分: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','')

答案 1 :(得分:3)

ginput函数获取图中moueclicks的坐标。您可以将它们用作直线,多边形等的点

如果这不符合您的需求,您需要详细说明您希望用户绘制的内容。

对于徒手画,这可能会有所帮助:

http://www.mathworks.com/matlabcentral/fileexchange/7347-freehanddraw

答案 2 :(得分:2)

我知道使用鼠标与matlab窗口交互的唯一方法是ginput,但现在这将让你绘制任何具有流动性的东西。

有很多方法可以在matlab中使用Java Swing组件检查http://undocumentedmatlab.com/以获取更多信息。

编辑:您可能也想检查一下。

http://blogs.mathworks.com/videos/2008/05/27/advanced-matlab-capture-mouse-movement/

相关问题