zoukankan      html  css  js  c++  java
  • Matlab获取鼠标坐标值的ginput()函数

    获取鼠标坐标值的第一种途径:利用Matlab7.0 中figure的WindowButtonDownFcn属性。当你在图上按下鼠标的时候,可通过该属性定义一个回调程序。回调程序可以是一个有效的Matlab表达式或者一个M文件。

    那么为显示当前鼠标按下时的坐标值,我们可以将其定义为一个坐标获取和显示程序。

    例如:
    % 主函数
    function test_mouse_track()
    figure;
    axis([-10,10,0,5]);
    set(gcf,'WindowButtonDownFcn',@ButttonDownFcn);

    % 回调函数
    function ButttonDownFcn(src,event)
    pt = get(gca,'CurrentPoint');
    x = pt(1,1);
    y = pt(1,2);
    fprintf('x=%f,y=%fn',x,y);

     

     

    Matlab的ginput()函数

    获取鼠标坐标值的第二种途径:

    ginput提供了一个十字光标使我们能更精确的选择我们所需要的位置,并返回坐标值。函数调用形式为:

    [x,y] = ginput(n)
    [x,y] = ginput
    [x,y,button] = ginput(...)

    对于[x,y] = ginput(n),能使你从当前的坐标系中读取n个点,并返回这n个点的x,y坐标,均为nX1的向量。可以按回车提前结束读数。

    [x,y] = ginput 可以无限的读取坐标直到按下回车键。

    [x,y,button] = ginput(...) 返回x和y的坐标,以及button值(1=左键,2=中,3=右)或者按键的ASXII码值。

     

     

    spline函数:

    yi=spline(x,y,xi);
    这个是根据己知的x,y数据,用样条函数插值出xi处的值。即由x,y的值计算出xi对应的函数值。

    而pp=spline(x,y);
    是由根据己知的x,y数据,求出它的样条函数表达式。

    一个小程序:

    axis([0 10 0 10]);
    hold on
    x=[];
    y=[];
    n=0;
    disp('单击鼠标左键点取需要的点');
    disp('单击鼠标右键点取最后一个点');
    but=1;
    while but==1
    [xi,yi,but]=ginput(1);
    plot(xi,yi,'bo')
    n=n+1;
    disp('单击鼠标左键点取下一个点');
    x(n,1)=xi;
    y(n,1)=yi;
    end
    t=1:n;
    ts=1:0.1:n;
    xs=spline(t,x,ts);
    ys=spline(t,y,ts);
    plot(xs,ys,'r-');
    hold off

  • 相关阅读:
    Codeforces 877 C. Slava and tanks
    Codeforces 877 D. Olya and Energy Drinks
    2017 10.25 NOIP模拟赛
    2017 国庆湖南 Day1
    UVA 12113 Overlapping Squares
    学大伟业 国庆Day2
    51nod 1629 B君的圆锥
    51nod 1381 硬币游戏
    [JSOI2010]满汉全席
    学大伟业 2017 国庆 Day1
  • 原文地址:https://www.cnblogs.com/nktblog/p/2658724.html
Copyright © 2011-2022 走看看