MATLAB: Does CONTOURF return NaN for some output values in MATLAB 7.7 (R2008b)

clabelcontourfcontourmatrixMATLABnan

When I run the following code with CONTOURF, I receive NaN for some output values.
 
[C,h] = contourf(peaks(20),10);
find(isnan(C)) % Displays the indices of NaNs in C.

Best Answer

This change has been incorporated into the documentation in Release 2009b (R2009b). For previous releases, read below for any additional information:
NaN values in the output of CONTOURF exist for degenerate lines, and contours that hit the edges of the axes and which CONTOURF forces to close.
The CONTOURF algorithm takes the original z matrix, then pads by wrapping an extra row on the top and bottom and an extra column on the left and right. These extra rows and columns are assigned significantly low values in order to enable interpolation at the boundary. Then the contour segments for each of the contour levels are calculated.
The CONTOURF algorithm postprocesses the resulting contourmatrix, and replaces the long contiguous boundary segments with NaNs so that downstream MATLAB functions handle rendering and other computation correctly
The contour matrix thus generated is parsed according to the documentation for CONTOUR/CONTOURF, i.e., each contiguous drawing segment contains the value of the contour, the number of x,y drawing pairs, and the pairs themselves. The result does not contain NaNs, but the result contains long contiguous segments that wrap around the outer boundary of the original z matrix. The CONTOURF algorithm then postprocesses the contour matrix, and replaces the long contiguous boundary segments with NaNs so that downstream MATLAB functions handle rendering and other computation correctly.