2

If I need to get the indices of some plotted points on a figure or axe by using box selection like the following:

load cities
education = ratings(:,6);
arts = ratings(:,7);
plot(education,arts,'+')

Plot demo image

How to get the indices of these points in the vector education not from the x axis ?

I want the solution to be flexible not for this plot only. I want to get the indices of any set of points, using box selection.

bla
  • 25,846
  • 10
  • 70
  • 101
Sameh K. Mohamed
  • 2,323
  • 4
  • 29
  • 55

1 Answers1

3

(i) If the # of points is small, you can use data cursor tool in the figure's gui.

(ii) You can use find or logical condition given some boundaries, for example:

  ind = find(arts>2e4 & education>2500 & education<3800);
  ans = arts(ind)`

so plot(education(ind),arts(ind),'ro') will show it:

enter image description here

(iii) you can select a box interactively with a imrect

h = imrect;
position = wait(h);

Then use the position (which is a vector of [xmin ymin width height]) values with find function:

ind =find(education>position(1) & education<position(1)+position(3) & ...
     arts>position(2) & arts<position(2)+position(4))

Edit:

After I was asked how polygon selection with impoly can be used, here is the solution:

h = impoly;
position = wait(h);
points_in= inpolygon(education,arts,position (:,1),position (:,2));
ind=find(points_in);
...
bla
  • 25,846
  • 10
  • 70
  • 101
  • I want it to be flexible not only for these points, I want to get the indices of some points using selection box. – Sameh K. Mohamed Jan 20 '13 at 06:19
  • see the bottom part of the answer – bla Jan 20 '13 at 06:22
  • that's a very nice answer, Can this be done also somehow using `impoly` ? – Sameh K. Mohamed Jan 20 '13 at 06:37
  • 1
    in `impoly` the `position` output is an n-by-2 array that specifies the initial position of the vertices of the polygon. `position` has the form [x1,y1;...;xn,yn]. So in order to only include the area you want you need a more sophisticated logical condition. The trick is that the # of vertices in the polygon is not known, so you need to automatically make the `find` meet the # of vertices. – bla Jan 20 '13 at 06:46
  • 1
    I've added a full solution to `impoly` – bla Jan 20 '13 at 07:33