1

I have a 2XN vector of points. I'd like to plot it and then enable the user to select one of the points by clicking on that point. I guess I can do it by myself by getting the mouse coordinates and select the point closest to them etc. I am wondering whether matlab provides a plug-and-play method for doing this?

olamundo
  • 23,991
  • 34
  • 108
  • 149

1 Answers1

1

There is no easy way to accomplish this, as far as I know. You can do one of the following:

  1. Check the distance and select the closest point (As you said yourself)
  2. Call the plot command N times, and assign a different callback for each plot.

In this case you create the graphics in the following way:

   for i=1:N 
      plot( X(i),Y(i), 'o', 'ButtonDownFcn', @(x)CallBack(x,i));
   end

And the callbacks look like that:

   function CallBack(x,i)
       fprintf(1,'A callback on P[%d] was called');
   end

If you want a special case of 2xN points: a draggable polygon, you can use the impoly command instead.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104