View License

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

» Watch video

Highlights from
MESH2D - Automatic Mesh Generation

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

5.0 | 33 ratings Rate this file 159 Downloads (last 30 days) File Size: 121 KB File ID: #25555 Version: 1.0
image thumbnail

MESH2D - Automatic Mesh Generation



Generates unstructured triangular meshes for general 2D geometry.

| Watch this File

File Information

MESH2D is a toolbox of 2D meshing routines that allows for the automatic generation of unstructured triangular meshes for general 2D geometry.

An iterative algorithm is used that attempts to optimise the mesh topology/vertex position to achieve high quality triangulations.

In addition to the fully automatic settings, MESH2D allows the user to specify sizing information, allowing for varying levels of mesh resolution within the domain.

MESH2D includes a large set of demos/benchmarks. Please see the following:

meshdemo - runs a standard set of demos.
mesh_collection - a collection of user contributed geometries.
facedemo - examples of geometry with connected faces.

MESH2D was previously released under a GNU license. This has been removed to cater for the current restrictions of the MATLAB file exchange. I apologise to the many users who have been unable to access MESH2D due to this license conflict.


This file inspired Fgt Fold Geometry Toolbox, Accelerating Finite Element Analysis (Fea) In Matlab, and Remove Unconnected Triangle.

MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (65)
21 Nov 2016 Meghan

Meghan (view profile)

Hi, excellent work on these tools. I wonder if you could help me with something. I'm trying to alter your refine.m function to allow for another parameter to be added. I have boundary codes (0-2) at each of the original nodes, I need for those codes to remain at the same nodes and populate the new nodes made from your function, i.e. one of my boundaries has all code 1 because it is a land boundary so I need all the newly created nodes on that boundary to have a 1 value.
Thanks, and again, excellent function :)

15 Nov 2016 Matthias Braendel  
01 Nov 2016 ll ll

ll ll (view profile)

Hi, I really appreciate for you work. However, i am wondering if this tool could generate mesh with the boundary node constant. That is the mesh generated with the input node (on the edge), the new node (on the edge) will not be added.

22 Sep 2016 Alexander Naitzat

Hello. Excellent tool. Where can I find data set of interesting 2D meshes, or data set of interesting polygons from which I can generate meshes ?

26 Jul 2016 Richard Crozier

there's a version of this with some bug fixes (e.g. missing tsearch) and more examples here:

Comment only
29 Jun 2016 Smitha Subeesh  
29 Jun 2016 Smitha Subeesh


This work is really awesome. Its very helpful for me. But I am facing problem with parabolic functions in distmesh2d.m
Please help me by giving an example of discretization of a domain which is an intersection of 2 parabolas in the 1st quadrant.


Comment only
12 Apr 2016 jun liu

Hi,I've got the same problem as Marcus's(Spe 19 2013),When I use meshfaces to get 2D unstructured mesh generation for polygonal geometry I get the message: 'Warning: Duplicate data points have been detected. Some data points do not map to vertices in the triangulation. To avoid this behavior, call UNIQUE on the data points prior to calling DELAUNAY.'
In delaunayn at 58
as follow:
if dupesfound warning(message('MATLAB:delaunayn:DuplicateDataPoints'));
I want to know how to deal with this problem. I appreciate if any one could help me.

Comment only
20 Jan 2016 Adrien LALISSE

Thank you for your work. I am trying to use it for meshing a star but I would like to get an uniform meshing;
Is there anyway to do that without using refine ?

Comment only
03 Dec 2015 Mario Koddenbrock  
22 Aug 2015 Felix

Felix (view profile)

Hello, first thank you for this great tool!
My question is, there is a solution to the problem with the deleted function dsearch in tinterp?

Comment only
29 Jun 2015 Rui Liang

Just want to inform Jeremy and Frederic that, read Mike's fix carefully. Do not just change "tsearch" to "tsearchn" but also notice the changes in the brackets after "tsearchn". It works for me.

29 May 2015 Jeremy

Jeremy (view profile)

Hey, Got the same errors as Frederic, and tried to fix them, but it's still not working!

Comment only
04 Apr 2015 Sergei Paleichuk  
03 Apr 2015 Frédéric Moresmau

Hi, I get error in meshdemo
I fixed the tsearch with the tserachn how (Spe 23 2013) suggsted.
Now the next error is

Error using fixmesh
Too many input arguments.

Error in meshfaces (line 157)
[p,t,fnum,fnum] = fixmesh(p,t,[],fnum);

how to fix all this ?


Comment only
03 Apr 2015 zheng kang

Thanks Darren for sharing this. Helps a lot!

27 Oct 2014 henripes

does anyone know why this hit me with this problem:
"Checking Geometry
Undefined function or variable 'wbar'.
Error in meshfaces (line 127)
Error in facedemo (line 39)
meshfaces(node,edge,face,hdata); "

Im trying to run this:

node = [0.0, 0.0; .4,0.0;0.2,0.5;0.4,0.5;0.2,1; 0.0,1; ...

edge = [1,2; 2,3; 3,4; 4,5;5,6;6,1;...

face{1} = [1,2,3,4,5,6];
face{2} = [7,8,9,4,3,2];
face{3} = [10,11,12,8];

hdata.hmax = 0.05;

which should be a sawtooth grating between air layers.

Comment only
23 Oct 2014 henripes

Hi, finally some clear meshing program also for beginners! Thanks! There is anyhow a problem with "DSEARCH" in mesh_collection(6). Is it anyhow possible to repair it?

Comment only
15 Oct 2014 Seth

Seth (view profile)

We have been using this function since early 2010 and has proved very valuable for creating basic 2D meshes for finite element analysis.

The only issue is that with newer releases (since R2012?) the "tsearch" function is no longer supported by Matlab.

We have used the fix proposed by Mike (23 Sep 2013) and it has worked reasonably well for our problems and was fairly simple to fix in the source code.

Thanks again Darren & Mike for the quick fix.

This type of submission is one reason the File Exchange is simply awesome.

Comment only
29 Sep 2014 Tanvir Faisal

there are few functions seem to be missing such as tsearch, pointLocation. any idea??

Comment only
10 Sep 2014 Martin

Martin (view profile)

01 Sep 2014 hust

hust (view profile)

unstructured mesh generator

Comment only
04 Aug 2014 Ademiloye Adesola  
04 Aug 2014 Ademiloye Adesola

Excellent work! I suggest Prof Darren should try to implement some of the bugs fixed in the comments below when preparing the newer version.

02 Jul 2014 KSSV

KSSV (view profile)

Excellent ...code..very useful for FEA..

17 Jun 2014 Putra Arri Sandhi  
22 May 2014 liron

liron (view profile)

you are a genius!!!! awesome addition... thanks very helpful....

10 Apr 2014 Thomas Benson

This is a great submission! A minor change was required for my application. Reason: The triangulation tends to be slightly over-refined on complex boundaries. For some applications this is a bonus, but for hydrodymanic modelling it is not really necessary. I therefore replaced the line in 'quadtree.m' (about line 64) which reads:

L = 2.0*dist2poly(wm,edgexy,2.0*len);

with the following:


This helps prevent over refinement and a nice mesh. Excellent work Darren!

26 Feb 2014 Abdulrahman  
03 Feb 2014 Abdulrahman

Great work thank you very much.

I've got a question I appreciate if any one could help, where exactly the changes can take place ? I know it would be most about the geometry but I wasn't able to play with it.

19 Dec 2013 Marcus

Marcus (view profile)

I'm doing something that must be pretty standard. I'm needing to mesh a 2d domain with an internal boundary. My approach has been to create 'holes' with zero area, which imposes duplicate nodes. Mesh2d seems able to remove these and gives e.g., the message 'WARNING: 2 duplicate node(s) removed'. However, with some of my examples I get the message: 'Warning: Duplicate data points have been detected. Some data points do not map to vertices in the triangulation. To avoid this behavior, call UNIQUE on the data points prior to calling DELAUNAY.' Now what I would like to know is whether this problem in the geometry is fixed by Mesh2d? I include a simple example of the sort of problem I'm interested in.
PS. Mesh2d has proved invaluable - Thank you!
p1 = [0 0; ...
3 0; ...
3 3; ...
0 3];
p2 = [0.5 1.5; ...
1.5 1.5; ...
1.5 2.5; ...
1.5 1.5; ...
0.5 1.5];
n1 = size(p1,1);
n2 = size(p2,1);
c1 = [(1:n1-1)', (2:n1)'; n1, 1];
c2 = [(1:n2-1)', (2:n2)'; n2, 1];
node = [p1; p2];
cnect = [c1;c2+n1];
hdata.hmax = 1;
[p,t] = mesh2d(node,cnect,hdata,[]);

Comment only
22 Oct 2013 Richard Clarke

Great routine, although I was having some issues removing redundant nodes until I changed the following in line 66 of changegeometry.m

del = nNode-i(end);


del = nNode-length(i);

Comment only
23 Sep 2013 Mike

Mike (view profile)

This is a powerful and useful tool. It is well written and documented.

Since Mathworks abandoned tsearch, simply calling tsearchn instead works for me. Moving to tsearchn requires two changes that I am aware of:

mytesearch.m Line 68:
i(j)=tsearchn([x y],t,[xi(j) yi(j)]);

meshfaces.m Line 199:
i = tsearchn(ph,th,p);

I do not agree with the solution proposed by Chen and Barnhart. Delaunay triangulations are not unique. Their approach effectively generates a separate triangulation and searches over it. Mapping such results into the original Mesh2D triangulation is ill-advised.

08 May 2013 Elias Pirayesh

There are some useful examples on meshing a 3D surface on:

Comment only
07 May 2013 Evgeny

Evgeny (view profile)

How can I use it on 3D surface?
Is there some algorithm to create unstructured triangular meshes for 3D geometry?

03 Apr 2013 William Barnhart

One more fix. In quadtree.m, near line 528 (right before %Undo rotation, p=rotate(p,-theta)), add:

% remove duplicate nodes
for i=1:length(goodid)

So that's three places to fix with the comments below:
meshfaces.m, line 203
mytsearch.m, line 69
quadtree.m, line 528 (or thereabouts)

Comment only
02 Apr 2013 William Barnhart


In my tsearch, replace:
i(j) = tsearch(x,y,t,xi(j),yi(j));

for k=1:size(t,1)

Then, no need to worry about delaunay triangulations

Comment only
02 Apr 2013 William Barnhart

Tsearch fix courtesy of Rowena Lohman.
tsearch.m needs to be replaced in two locations. First:

in meshfaces.m, line 203 replace:


for j=1:size(th,1)

In mytsearch.m line 69, replace:
i(j) = tsearch(x,y,t,xi(j),yi(j));


t = DelaunayTri([x y]);
i(j)= pointLocation(t,[xi(j) yi(j)]);

This uses DelaunayTri which is being fazed out, but delaunayTriangulation can be equally used

Comment only
20 Mar 2013 Thomas Benson

Hi Darren, thanks for this submission. Is there a version of Mesh2d that works with V2012b ? (TSEARCH.m no longer works and requires the use of DelaunayTri.m). Cheers.

Comment only
04 Oct 2012 Elliot

Elliot (view profile)

I too need an update to replace tsearch function. The suggested replacement is not appropriate for me as performing 'DelaunayTri' within your routine can alter the triangulation depending on initial inputs i.e. I have specified internal boundaries, DelaunayTri does not respect these boundaries when implemented in the suggested way.

Is a modified/updated version in consideration?


Comment only
17 Jul 2012 Song Chen

Find the following line:

p = node;
e = edge;
i = tsearch(ph(:,1),ph(:,2),th,p(:,1),p(:,2));
Change it to

p = node;
e = edge;
dt = DelaunayTri(ph);
i = pointLocation(dt,p);

These two codes should be equivalent.

Comment only
17 Jul 2012 Jody Klymak

Jody Klymak (view profile)

tsearch is gone as of 2012a. Any hope of a modification to use DelaunyTri?

Comment only
17 Jun 2012 cleidinei  
15 Jun 2012 Richard Crozier

I think Maxim is right:

>> A = triarea([0,0; 1,0; 0.5,1],[1,2,3])

A =


I think the area of that triangle should clearly be 0.5.

Comment only
16 Apr 2012 Maxim

Maxim (view profile)

I think, there are a bug in the function "triarea".
I miss a factor 0.5 in row 14.

Comment only
20 Aug 2011 Bryan

Bryan (view profile)


Thank you very much for this package, it is exactly what I'm looking for. I do have one question/comment. In Matlab 2011a, I get a warning that tsearch is going to be retired and to use delaunayTri/pointLocation isntead. I subbed in this set of commands (seemingly appropriately), and I cannot get the code to work correctly. Could you possibly advise on this? I'm going to use the code as is for the time being and try to implement and update. I'm not sure if you would be willing to use this update as a newer revision or are currently working on this problem.

Thank you,

Comment only
09 Aug 2011 Darren Engwirda

Hi Philippe, you may want to have a look at the utility "connectivity" routine. Amongst other things, it will identify the boundary nodes in a mesh.

Comment only
02 Aug 2011 Philippe N/A

Hi and ty for the great toolbox.

I'm using this to make a mesh that I want to use in another modeling tool. This other tool requires me to give nodes that are located on the edge of the mesh figure.

So, once the mesh is generated, is there any way to retrive nodes that have been generated on the contour of the figure?


Comment only
13 Jun 2011 Jose Luis Prego

Hi, thank you very much for your work. It looks impressive. However allow me to add a small suggestion. It would Very Nice if the Toolbox comes with a small info-note or Manual about the functions and how to start for a beginer. Appart from this you´ve made a Great Work!
Thank you again

23 Mar 2011 Brian Faulkner

Extremely helpful, this is a great library. Well documented and simple to use. Bravo sir, bravo.

26 Dec 2010 Diablo

Diablo (view profile)

Thanks so much for your file upload. This was invaluable to me

09 Aug 2010 Philippe N/A

Hi there.
First of all, thank you for that excellent toolbox.
I was wondering if it was possible to specify some point inside a geometric figure. For example, in the Lake Superior demo, I would like to insert a point in a specific location in the middle of the lake so the meshing is finer around that point. However, I can’t find how to do this. I bet I have to construct a new point (let’s say p8[x,y]) and connect it with existing points. But I get lost in that process.

Any help would be greatly appreciated.

With regards,

Comment only
09 Jun 2010 Greg

Greg (view profile)

I love your code, but I've recently begun some efforts to see if I can't speed up the runtime a bit.

Is there a particular reason why "mydelaunayn.m" uses the "delaunayn" function instead of the "delaunay" function? As far as I can tell on my machine (with Matlab 7.10), "delaunay" is around four times faster than "delaunayn" and the code seems to function just fine when I make the replacement.

06 Jun 2010 Marios Karaoulis

Why the following fails with meshfaces. I can run it with mesh2d

node = [0.0, 0.0; 1.0,0.0; 1.0,1.0; 0.0,1.0; 2.0,0.0; 2.0,1.0; 3.0,0.0; 3.0,1.0];
edge = [1,2; 2,3; 3,4; 4,1; 2,5; 5,6; 6,3; 5,7; 7,8; 8,6];
faces{1} = [1,2,3,4];
faces{2} = [5,6,7,2];
faces{3} = [8,9,10,6];
hdata.edgeh=[1,0.05 ;7,1];

running it with meshfaces

??? Index exceeds matrix dimensions.

Error in ==> meshpoly>getedges at 283
bnd = e(~idx,:);
% Boundary edges

Error in ==> meshpoly at 88
e = getedges(t,size(p,1));
% Unique edges

Error in ==> meshfaces at 147
[pnew,tnew] =

Comment only
31 May 2010 Johannes Korsawe

Very nice tool! Thank you a lot for sharing this.

Best regards, Johannes

Very minor irritation:
In line 127 of meshfaces.m, the variable wbar is addressed but never defined.

15 Apr 2010 Canh Le

This is what I was looking for. Many thanks!!

11 Mar 2010 Ralph

Ralph (view profile)


Thank you for this great work. Really, really helpful!

I need to ask a question: When using meshfaces, is it possible to use a different 'hfun' for each face?


10 Mar 2010 Robert Moucha

Many thanks,

Any plans to bypass MATLAB's tsearch?
In the 2010a release "Warning: TSEARCH will be removed in a future release. Use DelaunayTri/pointLocation instead."

17 Feb 2010 johnus magnus

Just a great collection of functions, thankyou!

19 Jan 2010 Seth

Seth (view profile)

17 Jan 2010 Wes

Wes (view profile)

I would like to generate a mesh with certain nodes specified on the interior. When I try to do this I end up with areas of my domain that are not filled with elements. For example, I would like node[0.25 0.5] connected to node[0.75 0.5] and the mesh to fill the entire region from [0 0;1 0;1 1;0 1]. The actual meshes I would like to make have more complex node positions.

The following produces a hole in the mesh:

node=[0 0;1 0;1 1;0 1;0.25 0.5;0.75 0.5];

18 Oct 2009 Marios Karaoulis  
18 Oct 2009 Marios Karaoulis

Do you have any thoughts going 3D?

Comment only
14 Oct 2009 Darren Engwirda

Author comment: v24 was posted to resolve a licence conflict. The Mathworks no longer allows code to be released under a GNU GPL, so this has been removed for v24.

There are some (minor) revisions to the code, based on user feedback. Specifically, v24 may be slightly slower than v23, but should generally produce higher quality meshes, especially for complex geometries.

Comments/feedback is always welcome -

Comment only
14 Oct 2009 Marios Karaoulis

Hi, thanks again for your software. What's new in this version ( i am using v23).

Comment only

Contact us