File Exchange

image thumbnail


version 1.1 (9.39 KB) by

Contouring and filled contouring for unstructured 2D triangular meshes



View License

TRICONT and TRICONTF have functionality equivalent to that of CONTOUR and CONTOURF, but the surface is defined over a 2D mesh of triangular elements (which may have a non-convex hull and/or may contain interior holes).
TRIDEMO gives a demonstration.

Comments and Ratings (13)

Rich Pawlowicz

re: clabel - unfortunately, clabel calls will no longer work in matlab2014b and later. This is because you can no longer 'trick' clabel by faking up the kind of output that a call to 'contour' returns.


Silvio (view profile)

Very nice code, I have just a problem. when I want to put a label on tricontf I got this problem:
Error using clabel (line 70)
Not enough contour handles.

The code I am running is this:
[C1,h2]=tricontf(x,y,tri,df,scala) ;
where scala is a vector for the levels.
The contour plot appears smoothly, just clabel is not working. I am running matlab 2014b on a mac

Rich Pawlowicz

If it is an unstructured mesh than the hole is implicitly defined by a lack of triangles in it. So, short answer - yes, that's feasible.


Hello Rich,
thanks for the submission. I have an unstructured mesh with some holes in the middle and I don't have a mathematical formulation which defines their position. Would that be something feasible with your code?


Ryan (view profile)

Fixed - required changing > to >= in a couple of places (darned special cases)


Ryan (view profile)

To amend the comment below, I see now that the first error occurs if any level is specified as max(Z) (e.g. substitute the line below).
v = sort([-10:10 min(Z(:)) max(Z(:))]);


Ryan (view profile)

These are great functions, thanks for the submission!

Sometimes, I encounter problems with tricontf. It seems that if the levels vector is specified so that the last level is exactly max(Z), tricontf gives an error at line 194. However, I do not get errors from tricont, contour, or contourf. For example:

[X,Y,Z] = peaks(100);
DT = delaunayTriangulation(X(:),Y(:));
M = DT.ConnectivityList;
v = linspace(min(Z(:)),max(Z(:)),21);
subplot(221); contour(X,Y,Z,v); title('contour')
subplot(222); tricont(X(:),Y(:),M,Z(:),v); title('tricont')
subplot(223); contourf(X,Y,Z,v); title('contourf')
subplot(224); tricontf(X(:),Y(:),M,Z(:),v); title('tricontf')
axis(findall(gcf,'Type','axes'),'equal',[-3 3 -3 3])

If I perturb the max(Z) in the levels, I do not get an error (e.g. substitute the line below), but that is not an ideal fix.
v = linspace(min(Z(:)),max(Z(:))+1e-10,21);

Also, tricontf produces an error again at line 194 if Z is constant, whereas tricont produces an empty plot and both contour and contourf throw a warning and produce an empty plot.

An update to fix these issues would be greatly appreciated!

Xinwen Li

the work is wonderful!
for most of time I get smooth contour lines.While sometimes the there exist "saw-tooth" on the contour lines


rmc256 (view profile)

Works wonderfully, better than previous submissions (tricontour). The contours of my mesh were being split with tricontour (produced two contours, even though same level and connected), but tricont produces the correct contours. Saved me a lot of debug time.


Victor (view profile)

Works well! Thanks!

Thomas Benson

I've been waiting for someone out there to do triangular mesh filled contours for ages now (I did have a go a trying to code myself but found it very tricky to make it generic). Hats off to you Rich. I haven't tested thoroughly but tidiness of code and what it promises to do means I'll give it 5 anyway!



Fixing reported issues required changing > to >= in a couple of places (darned special cases)

MATLAB Release
MATLAB 7.7 (R2008b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video