MATLAB: ‘inpolygon’ returns zeros logical variable (R2019b, Win10)

getptsinpolygonmaskMATLABr2019b

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 420×1380 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?

Best Answer

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.)