Points inside multiple polygon for contour
Show older comments
I made the following contourplot.

Then, I plot discretized points over this plot.

I wanted to find the (x,y) coordinates of the discretized points within the region between the boundaries 40.94 and 43. This is the way how I approached it. With contourtable I acquired the (x,y) coordinates of the contourlines.
muSP = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9];
muRP = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9];
[X,Y] = meshgrid(muSP,muRP);
Z = cell2mat(reshape(AoR_mean,[9,9]));
[C,h] = contour(X,Y,Z,(40.9431:2.0538:42.9969));
hold on
title('Angle of Repose - Ledge Test 2D');
xlabel('\mu_s')
ylabel('\mu_r')
clabel(C,h,'LabelSpacing',500,'FontSize',8);
h.LevelList = round(h.LevelList,2);
set(gca,'XTick',0.1:0.1:0.9,'XTickLabel',0.1:0.1:0.9,'XTickLabelRotation',45);
set(gca,'YTick',0.1:0.1:0.9,'YTickLabel',0.1:0.1:0.9);
grid on
% Extract x- and y-coordinates of contour lines
contourTable = getContourLineCoordinates(h);
% Define grid for the variable space
N1 = 80;
muRP_grid = linspace(0.1,0.9,N1);
muSP_grid = linspace(0.1,0.9,N1);
[R,S] = meshgrid(muRP_grid,muSP_grid);
plot(R,S,'.r');
hold off
% x- and y-coordinates of polygon vertices, i.e. between AoR of 40.11 degrees
% and AoR of 43.83 degrees
xv = table2array(contourTable(1:31,3));
yv = table2array(contourTable(1:31,4));
inside_variablespace = inpolygon(R,S,xv,yv);
% x- and y-coordinates corresponding to the discretized points in the
% variable spac
xcoord = R(inside_variablespace);
ycoord = S(inside_variablespace);
However, for some reason I don't get de discretized points within the boundaries 40.94 and 43.

Accepted Answer
More Answers (1)
Ameer Hamza
on 29 Sep 2020
There is no need to use inpolygon here. You can directly create a mask from the Z matrix. Following shows an example, and find the x and y coordinates from the mesh between contour lines at 0.33 and 0.66
[X, Y] = meshgrid(linspace(-1, 1));
Z = X.^2 + Y.^2;
levels = [0 0.33 0.66 1]; % levels for contour plot
mask = Z < 0.66 & Z > 0.33;
x_coord = X(mask);
y_coord = Y(mask);
contour(X, Y, Z, levels)
hold on
plot(x_coord, y_coord, '+')
4 Comments
Tessa Kol
on 29 Sep 2020
Ameer Hamza
on 29 Sep 2020
You will write it like this
mask = Z < 43 & Z > 40.97;
x_coord = X(mask);
y_coord = Y(mask);
The x-y coordinates will be extracted from corresponding locations in X and Y matrix.
Tessa Kol
on 29 Sep 2020
Ameer Hamza
on 29 Sep 2020
What is the error. In the data you shared, AoR_mean is undefined, so I cannot run your code to see the issue.
Categories
Find more on Contour Plots 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!

