'inpolygon' returns zeros logical variable (R2019b, Win10)

Hi,
I try to make a mask for area of ocean's basin using function inpolygon.
I have a big area with latitude from 66° to 73° (length is 420) and longitude from -5° to 18° (length is 1380):
lat = linspace(66,73,420)'; % latitude
lon = linspace(-5,18,1380)'; % longitude
[LAT,LON] = ndgrid(lat,lon); % 2-D matrix
Next, I use contourf to plot bathymetry ('z' has 420x1380 size) and choose mask border with mouse (polygon vertices):
contourf(lon,lat,z) % bathymetry
[xi,yi] = getpts(gca); % choose mask border
xi = [xi; xi(1)]; yi = [yi; yi(1)];
hold on
plot(xi, yi,'k','LineWidth',2) % polygon (mask)
So i get something like this:
Finally, I use inpolygon to check what points (grid nodes - LAT and LON) belong to this polygon. But there is no 'true' points inside (variable 'check' is zero).
mask = inpolygon (LAT, LON, xi, yi);
check = sum(sum(mask))
Anyone have any ideas how to fix this?

1 个评论

I still don't know what problem is but I found another solution without 'inpolygon':
lat = linspace(66,73,420)'; % latitude
lon = linspace(-5,18,1380)'; % longitude
[LAT,LON] = ndgrid(lat,lon); % 2-D matrix
LAT = LAT(:); LON = LON(:); % 2-D to 1-D
contourf(lon,lat,z) % bathymetry ('z' is depth of the ocean)
[xi,yi] = getpts(gca); % choose mask border with mouse
pgon = polyshape(xi,yi); % 2-D polygons
TFin = isinterior(pgon,LON,LAT); % query points inside polyshape
mask = reshape(TFin,[420,1380]); % from 1-D to 2-D
hold on
plot(xi, yi,'k','LineWidth',2) % polygon (line)
plot(LON(mask),LAT(mask),'r+') % points inside polygon
Maybe it isn't the best solution but it's work.

请先登录,再进行评论。

 采纳的回答

Note that longitude is horizontal and latitude is vertical, so you'd need to swap LAT and LON in your call to inpolygon. (Like you have in your call to isinterior in the solution you found.)

3 个评论

Yes, longitude is horizontal, so this's 'x' (and latitude is 'y'). That's why I use LON at the first place.
You have LAT first in your call to inpolygon:
mask = inpolygon (LAT, LON, xi, yi);
check = sum(sum(mask))

请先登录,再进行评论。

更多回答(0 个)

产品

版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by