Question

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.

Was it helpful?

Solution

(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);
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top