3

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..

Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62
user129412
  • 765
  • 1
  • 6
  • 19
  • Why not remove the "nearby" clicks after exiting the loop when you run `ginput` for `unlimited number of points` option? – Divakar Mar 20 '14 at 20:11
  • I'm not sure I follow. What do you mean by running ginput in an infinite loop? And do you mean manually remove then? – user129412 Mar 20 '14 at 20:13

1 Answers1

2

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
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • That does seem to do the trick. I'm having some trouble understanding what exactly it does. It computes the difference between adjacent points, and checks if this is smaller than a certain tolerance? I'm not sure exactly how the sum comes in there. Can't it work using only the difference? Thanks for the help, in any case! – user129412 Mar 20 '14 at 20:50
  • The `sum` is basically sum of differences between the `x` and `y` values. One can use `min` or any other other deviation measuring scheme. Now, I have realised that I must have used sum of absolute differences. Would update with it. – Divakar Mar 20 '14 at 20:53
  • I understand now. Thanks a lot for the answer, clever indeed! Exactly what I was looking for. – user129412 Mar 20 '14 at 21:02
  • Check out the naive approach (just edited) for lesser hassle from user-point of view and in this code, number of points to be clicked can be specified. – Divakar Mar 20 '14 at 21:14