MATLAB: How to make contourf plots beyond a polygon invisible

inpolygon

I use the below to plot a contourf of a geographic region (imagine it is the Mediterranean Sea).
[X, Y] = meshgrid(linspace(min(lon),max(lon),100),linspace(min(lat),max(lat),100));
Z = griddata (lon, lat, variable, X, Y);
[C, h] = contourf (X, Y, Z, [1:0.1:4], 'LineStyle', 'none' );
The problem is that the contour extends beyond the sea into the land. How do I hide the contourf portions that extend beyond the boundary (polygon)? Thanks.

Best Answer

Use patch to overlay white on top.