Creating a cell for each plot grid

3 views (last 30 days)
Hi guys! I'm struggling with this problem.
At first I would like to display an empty graph spaced by 2. For this purpose both axes have value from 0 to 10 stepped by 2.
Secondly, I would like to associate to each subgrid to a square polygon let's assume P1=(0,0) P2=(2,0) P3=(0,2) P4=(2,2).
At this point I would like to check for whichever point in the space if they belong or not to this polygon.
If yes, I need to save in a cell/table all information related to the point namely X and Y coordinates and timestamp.
Thanks to all in advance.

Accepted Answer

Adam Danz
Adam Danz on 28 Jul 2021
At first I would like to display an empty graph spaced by 2. For this purpose both axes have value from 0 to 10 stepped by 2.
After generating the axes, set the XTick and YTick to 0:2:10.
Secondly, I would like to associate to each subgrid to a square polygon let's assume P1=(0,0) P2=(2,0) P3=(0,2) P4=(2,2).
I interpret this as I would like to add a polygon to the axes. If the polygon is rectangular and parallel to the axes use
rectangle('position',[L,B,W,H])
where L and B are the (x,y) coordinates for the left, bottom corner and W and H are width and height.
Otherwise you can use polyshape or patch.
At this point I would like to check for whichever point in the space if they belong or not to this polygon.
If yes, I need to save in a cell/table all information related to the point namely X and Y coordinates and timestamp.
Use the outputs of inpolygon in a conditional statement. Set up a table that has a column for the variables you listed above and add values to the table as needed.
If you get stuck on any of the steps above, share your updated code and point out the problem so we can help get you unstuck.
  4 Comments
Riccardo Tronconi
Riccardo Tronconi on 30 Jul 2021
Yes, in this early stage I do not need any visualization so just to know where the point lies.
the second question is how to assign a point in a quadrant. To do so I have created two arrays as it follows:
for i = 1:length(p)
pos(:,i)= get(p(i), 'Position');
xv(:,i)=[pos(1,i)+0, pos(1,i)+pos(3,i), pos(1,i)+pos(3,i), pos(1,i)+0]'
yv(:,i)=[pos(2,i)+0, pos(2,i)+0, pos(2,i)+pos(4,i), pos(2,i)+pos(4,i)]';
end
xv and yv cointais the vertices related to each polygon.
At this point, I have a cell (3x1) in which each row represent represent a position in X and Y of a given subject (1,2,3) and the related timestamp. The following code is useful to assign each point to its related subpolygon.
for j=1:n % loop over each element of the cell T{1}, ..., T{3}
for k= 1:height(T{j}) % loop over each row of T{j}
for z = 1:length(p) % z=1:25 which is the total number of my possible vertices
if inpolygon (T{j}{k,2}, T{j}{k,3}, xv(:,z)', yv(:,z)')==1 % check where the point is contained
Spg{z}{k,1}= T{j}{k,1};
Spg{z}{k,2}= T{j}{k,2};
Spg{z}{k,3}= T{j}{k,3};
end
% Spg{z} is a cell array for all subpolygons and for this purpose
% would contain every point which belong to a particular polygon.
% es. P1=(0,54; 0.99) is contained in Spg{1}
end
end
end
This code with several nested loops works but is very very slow to perform. Is there a much faster way to do that?
Adam Danz
Adam Danz on 31 Jul 2021
Since you don't need to display the rectangles, you just need to know which rectangle a dot belongs to.
That's what histcounts2() does. Here's a demo.
% Create random variable within the x & y extent
rng(210731) % for reproducibility
xy = rand(1,2)*10
% Determine where the random coordinate is within
% a grid of 2x2 rectangles extending from 0 to 10.
N = histcounts2(xy(1), xy(2), 0:2:10, 0:2:10)
This shows that coorinate (4.15, 7.06) falls into grid (3,4).

Sign in to comment.

More Answers (0)

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!