function MakePatchPlot(z,range,meshstruct)
% MakePatchPlot(Z,RANGE,MESHSTRUCT) 
%
% This function accepts a vector Z, a scalar RANGE, and a structure MESHSTRUCT including
% coordinates and connectivity. It then plots Z in the current axes on 
% an element-by-element basis (patch plot). RANGE is used to determine the
% limits on the colorbar.

% initialize variables
meshdata=meshstruct.size;
numel = meshdata(1);
nnpe = meshdata(3);
x=meshstruct.xcoords;
y=meshstruct.ycoords;
np=meshstruct.connect;

% find appropriate colorbar range
cmax = max(z);
cmin = min(z);
if abs(cmax-cmin)<(range*.001)
    tmp=0.10*range;
    cmax=cmax+tmp;
    cmin=cmin-tmp;
end

% prepare axes
a = [cmin cmax];
caxis(a);
axis equal;
hold on

% plot one patch at a time
for  i=1:numel
    for j=1:nnpe
        j1=np(i,j);
        xvertex(j) = x(j1);
        yvertex(j) = y(j1);
    end
    zval  = z(i);
    patch(xvertex,yvertex,zval,'EdgeColor','none');
end
hold off
h=colorbar('EastOutside');
if ~ispc
    set(h,'FontSize',14);
end
