MATLAB: Contourc – plotting contour matrix

contourcontour matrixcontourc

I would like to find the contours, manipulate them, and then plot them.
This plots contours
x = 0:0.1:1; y = 0:0.1:1;
[X,Y] = meshgrid(x,y);
Z = sin(X).*cos(Y);
[con_mat, h] = contour(x, y, Z);
To get the contour information without plotting, can use contourc.
con_mat = contourc(x, y, Z);
However, there appears to be no built-in way to plot this "contour matrix".
Any thoughts?
John

Best Answer

Try this: it uses cplot, which you can find on Matlab central.
If you find something more elegant, please let us all know... it MUST be buried in the Matlab library function SOMEWHERE.
function[] = plot_contourmatrix(C);
% plot_countourmatrix - Plots a contour matrix c as returned from contour
%
% plot_contourmatrix(C)
% Rev History:
% 06-04-12 Created
figure(gcf);
holdstate = ishold;
hold on;
i = 1;
while i<=length(C)
lev = C(1,i);
cnt = C(2,i);
cplot(C(1,i+(1:cnt)),C(2,i+(1:cnt)),repmat(lev,1,cnt));
i = i+cnt+1;
end;
if ~holdstate
hold off;
end;
return
test code - execute as cell
figure(1);
[x,y] = meshgrid(-3:.1:3);
z = sinc(hypot(x,y));
[C,h] = contour(x,y,z);
axis equal
figure(2)
plot_contourmatrix(C);
axis equal