MATLAB: Contour plot – how to show contours of irregular body in XY plane

contourinpolygon

Hi all
I use contour(X,Y,Z,n) function and it works fine. However, sometimes I would like to show only one part of the contours which are in top view (X-Y plane) within body of irregular shape. Something similar like "inpolygon".
Is anybody having an idea how to show only contours within selected area?
Thanks, Janez

Best Answer

You could replace the values that you don't want with NaNs. For example:
[x,y,z] = peaks;
subplot(121)
contourf(x,y,z)
z(abs(z)<0.5) = nan;
subplot(122)
contourf(x,y,z)