获取当前鼠标按键的坐标值,左键输出当前点,右键则删除上一个按键值,中间键退出

来源:互联网 发布:传媒大数据 编辑:程序博客网 时间:2024/05/22 13:42
clear,close all, clcx = 1:0.1:10;y = 20*sin(pi/2*x);figureplot(x,y);points = get_mouse_point()


-----------------------------------------------------

function points = get_mouse_point()% Author:shizhixin       % Email:szhixin@gmail.com       % Blog:http://blog.csdn.net/shizhixin       % Date:2013-03-08   % Function:获取当前鼠标按键的坐标值,左键输出当前点,右键则删除上一个按键值,中间键退出% Note:%注意不能按键太快,否则认为是双击会导致出错hold onflag=1;%(1=左键,2=中,3=右)points = [];while flag == 1    [x,y,flag] = ginput(1);%获取当前按键的坐标及按键值    if flag == 1 %如果为左键,用红色画当前点,并且保存当前点的值进入矩阵        plot(x, y, 'r*');        points = [points;[x, y]];        disp 'save current point';    elseif flag == 3 %如果为右键,矩阵中有值的情况则为删除操作,删除上一个点,在图中标为品红色        if ~isempty(points)            plot(points(end,1),points(end,2),'m*');            points = points(1:end-1,:);            disp 'delete';        end        flag = 1;    else%鼠标中间键为退出操作        disp 'end'    endend



原创粉丝点击