No License

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

» Watch video

Highlights from
Contours for triangular grids

5.0 | 17 ratings Rate this file 59 Downloads (last 30 days) File Size: 341 KB File ID: #10408 Version: 1.0
image thumbnail

Contours for triangular grids


Darren Engwirda (view profile)


17 Mar 2006 (Updated )

Generate smooth contours for functions defined on unstructured triangular grids

| Watch this File

File Information

Plots the contours of a function defined on an unstructured triangular grid directly, without the need to interpolate back onto a Cartesian array.

I have attempted to replicate much of the functionality of the regular "contour" function.

Type "help tricontour"

Type "contourdemo" for some examples

You may also find my mesh generator "mesh2d.m" in the physics and Chem section helpful.

UPDATE 11/05/2006
- Can now be used with clabel
- A bit faster

UPDATE 15/05/2006
- Much more accurate (now uses quadratic interpolation)


This file inspired Trisurfc and Trigradient.M.

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (25)
21 Jun 2016 Zoi T.

Zoi T. (view profile)

Hello, and thank you for this contour plot method! The functionality missing for triangle grids in MATLAB is indeed a bit frustrating. I have a small problem with this method though. I am using MATLAB 2015b, and I guess there has been some changes in the contour handle in the meantime. In particular the patch array, which is returned from the tricontour method creates a "Not enough contour handles" error in the clabel-call. MATLAB's contour method returns a handle that is a "contour"-object. I couldn't figure out much about how this object is created, it seems I have no access to the method that is creating the handle. Does anyone have a work-around this?

Comment only
06 Jul 2014 Manuel A. Diaz

04 Feb 2013 Michael

Great! any plans to make a version with filled contours? (like contourf) that'd be even better!

30 Jan 2013 Kim Nguyen

could somone please help, ive tried following the examples but it keeps telling me i dont put in enough arguments.
i have x,y, and dz as my coords

Comment only
26 Sep 2012 Nachiket Patil

21 Aug 2012 David

David (view profile)

Thank you Darren. I appreciate your superb contribution.

09 May 2012 l l

l l (view profile)


19 Feb 2012 lekshmi

18 Jul 2011 Ben

Ben (view profile)

Why I got some artifacts when using my own triangulated mesh? It generated some "contours" at the places where no triangles exist. Does anybody know why?

The mesh is no problem; I double checked.

Comment only
10 Jun 2011 Yan

Yan (view profile)

10 Jun 2011 illoul lounes

very usful, must be integrated to matlab.

thanks !

09 Aug 2010 Steve Fitzgerald

I'm sure this will be 5* when I get it to work; excuse my ignorance. I have a triangular mesh and x,y,z data that I plot with

trisurf(tri, x, y, z)

and I tried

[c, h] = tricontour([x y], tri, z, 15)

but I get an error where it tries flag(NaN). I'm pretty sure my data is OK, and trisurf works fine...

Comment only
29 Jul 2010 Xavy Kong

Sorry I missed three pnts when I shortened the lines. It should be like this.
A=[0 0 .5;1 0 .6; 1 1 .7;1 1 .7;2 1 .6;2 2 .5;1 1 .7;2 0 .4;2 1 .6];
x=A(:,1); y=A(:,2); z=A(:,3);
P=[x(:),y(:)]; T=[1,2,3;4,5,6;7,8,9];
I tried to use Fepoint format as in tecplot, but your suggestion not using pnt no.4 will plot the contour lines between my triangulars not within them. I am sorry that I did not quite understand the clauses arround 'bnd(next) = ce; % Found bnd edge' . Anyway, you have done a very nice job and I really appreciate your contribution and your assistance to me. I can try to fix my own problem later.

Comment only
28 Jul 2010 John D'Errico

John D'Errico (view profile)

In the test case given by Xavy, note that there are a pair of points at (x,y) == (1,1). This will cause any such routine to fail that is based on triangulating the data.

With replicates in your data, remove them, averaging the z values at those replicated points.

Of course, even this will not help your example, since you then try to create a triangulation

> T=[1,2,3;4,5,6;7,8,9];

You only have SIX points in the set of point in A. Surely you cannot create a triangulation that refers to points 7, 8 and 9 too! A valid triangulation of this data would be

T = [1 2 3;2 3 5;3 5 6];

See that I never needed to use point 4, but also that this is a valid triangulation. I would expect tricontour to work properly with this triangulation.

Comment only
28 Jul 2010 Xavy Kong

A=[0 0 .5;1 0 .6; 1 1 .7; 1 1 .7; 2 1 .6; 2 2 .5];
x=A(:,1); y=A(:,2); z=A(:,3);
P=[x(:),y(:)]; T=[1,2,3;4,5,6;7,8,9];
[c,h] = tricontour(P,T,z,3); % why not work ?
T = delaunayn(P);
[c,h] = tricontour(P,T,z,3); % % why not work ?
% This has to be improved?????????????

Comment only
01 Sep 2009 Jack Kohoutek

19 Apr 2009 Nik F

Nik F (view profile)

03 Feb 2009 Clemens Moser

Works very well, there is just a minor issue: the contours are not interpolated at the boundaries of the surface .

13 Jan 2007 Friedemann Groh

08 Aug 2006 Rabi Tahir

Works well... seems to be a bit slow, but I may be showing a bias (comparing with traditional fortran generated machine code)

05 May 2006 prabhakaran sundhararaju

it's very usefull for chemical, mechanical and civil. to analysis critical domain. i wish to develop 3D domain also

Comment only
29 Mar 2006 Babak Asghari

21 Mar 2006 Kong Zuor

This is what I have been looking for since I start using Matlab. Great job well done! I wish to see this code in the next Matlab release. I find this code very useful to those who solve CFD problems with matlab. The author deserve my five star ranking. I would encourage the author to advance this code for 3D case. I am free at last!
Many thanks

21 Mar 2006 M. Oswald

Very good contribution. I wish the author could extend the tricontour function to handle 3D triangle meshes as well.

20 Mar 2006 John D'Errico

Very nice. This is a function that should have been in Matlab itself since the day they provided trisurf and trimesh. It handled some troublesome cases I tried when I tried to break it. I'll give it a 5 rating because it deserves that rating. My only wish is that it was even more compatible with the contour functionality of matlab. For example, no handles are returned, in case you wish to set a line color or style. It might also be nice to provide a way to label the contours via clabel.

11 May 2006

clabel support

Contact us