gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:31:43 25 4
gpt4 key购买 nike

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

最佳答案

最后我找到了一个很好的代码,并且我更改了一些部分以便为我定制。通过这种方式,用户可以用鼠标在轴上绘制任何东西:

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

关于matlab - 在matlab中用鼠标在GUI上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12536376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com