Search code examples
matlabdouble-clickginput

Ignore double click with ginput in MATLAB


My question is rather simple, does anyone know a way to have matlab's ginput ignore subsequent clicks at the same location?

I've thought of some possibilities, like a for loop that checks the stored arrays for identical values and remove them, but then I run into trouble with the length of the for loop (as the arrays change size by removing stuff), so that doesn't work. I suppose there should be some easy workaround, but I haven't been able to figure one out yet..


Solution

  • Simple code but little bit more user effort version

    %%// Tolerance
    TOL = 5;
    
    %%// Start selecting points for an unlimited number, until Return key is pressed
    [x y ] = ginput;
    xy = [x y];
    xy(sum(abs(diff([x y])),2)<TOL,:) = []; %%// Remove the "nearby points"
    

    Naive and safer approach

    %%// Tolerance
    TOL = 5;
    
    %%// Number of points to be clicked
    N = 4;
    
    %%// Clicked points array to be stored here
    xy_all = zeros(N,2);
    
    %%// First point
    [x y ] = ginput(1);
    cmp1 = [x y];
    xy_all(1,:) = [x y];
    
    %%// Second point onwards
    k=2;
    while k<=N
        [x y ] = ginput(1);
        if sum(sum(abs([x y]-cmp1)))>TOL
            cmp1 = [x y];
            xy_all(k,:) = [x y];
            k= k+1;
        end
    end