No License

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

» Watch video

Highlights from

4.5 | 15 ratings Rate this file 130 Downloads (last 30 days) File Size: 4.14 KB File ID: #4512 Version: 1.0



Bill McDonald (view profile)

  • 1 file
  • 4.46667


20 Feb 2004 (Updated )

Write STL file from surface data.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

SURF2STL Write STL file from surface data.
    SURF2STL('filename',X,Y,Z) writes a stereolithography (STL) file
    for a surface with geometry defined by three matrix arguments, X, Y
    and Z. X, Y and Z must be two-dimensional arrays with the same size.
    SURF2STL('filename',x,y,Z), uses two vector arguments replacing
    the first two matrix arguments, which must have length(x) = n and
    length(y) = m where [m,n] = size(Z). Note that x corresponds to
    the columns of Z and y corresponds to the rows.
    SURF2STL('filename',dx,dy,Z) uses scalar values of dx and dy to
    specify the x and y spacing between grid points.
    SURF2STL(...,'mode') may be used to specify the output format.
      'binary' - writes in STL binary format (default)
      'ascii' - writes in STL ASCII format
    See also SURF.
    Author: Bill McDonald, 02-20-04


This file inspired Surf2solid and Stlwrite(Filename, Varargin).

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 (26)
06 Jan 2017 John Pope

filename='link0_test.stl'; %base
cyl_height = 0.1;
precision = 40; % increase to obtain a more accurate drawing

%create a unit height cylinder with 100 points. Radius 75
[X,Y,Z] = cylinder([cyl_radius], precision);
%Multiply Z by height

%Save in stl format, create new file
surf2stl(filename, X, Y, Z, 'ascii', 'w');

%now create a disk, bottom
radius = linspace(0,cyl_radius,precision); % For ten rings
theta = (pi/180)*[0:15:360]; % For eight angles
[R,T] = meshgrid(radius,theta); % Make radius/theta grid
X = R.*cos(T); % Convert grid to cartesian coordintes
Y = R.*sin(T);

%append this solid to already created file, bottom disk
surf2stl(filename, X, Y, 0.*X, 'ascii', 'a+');
%top disk
surf2stl(filename, X, Y, cyl_height.*ones(size(X,1), size(X,2)), 'ascii', 'a+');

Comment only
06 Jan 2017 John Pope

forget previous comment. digging through google results.... supposedly you need a meshgrid

[X,Y] = meshgrid(-2:.2:2, -4:.4:4);
Z = X .* exp(-X.^2 - Y.^2);

Comment only
06 Jan 2017 John Pope

This really could be simplified by moving the dimension detections within function (size(L):rows,size(L):cols)

here's how I got it working....

L needs to be 2 dimensional (rows,cols) l!!!!!

L = -0.1136 -0.1932 -0.0031 0.0593 -0.2272
0.2525 0.5000 0.9647 1.0000 0.0593
0 0 0 0.9647 -0.0031
0 0 0 0.5000 -0.1932
0 0 0 0.2525 -0.1136


Comment only
27 Oct 2016 Mychal Taylor

I'm trying to convert my .m file to a .stl file and I am having a hard time using this function. What do I put into the x y z componets in the surf2stl?

Comment only
28 Dec 2015 Tyler Moore

Is there a way to create a volume using this script? I have the .stl surface, but want to extend that to some "ground" so I can print it with a 3D printer.

Comment only
01 Oct 2015 Carlos Bezerra

Hi. I have cute solids by using trisurf(tri,xmesh,ymesh,zmesh,colorVector), where x(nx1), y(nx1), z(nx1) and tri(n/2 x 3). But, unfortunately it does not run at your surf2stl. Do you have any idea how can I run trisurf2stl? Thank you.

Comment only
07 Oct 2014 Arnaud GUILLOU-CAMARGO

17 Jun 2014 Thomas-Glyn Cox

Does not work anymore

17 Apr 2014 Wesley

Wesley (view profile)

Hi, I am trying to get MATLAB to output STL files of chaotic systems (Rossler, Lorenz, Chua). The graphs are made from iterating x, y, and z in a for loop. So I converted the vectors into a matrix using the vec2mat command. It gave me the STL files but the lines have no thickness. How can I resolve this problem? I tried adding space below the graph but it just makes a cylinder shape and doesn't thicken the lines. Please advise.

20 Feb 2014 Adam

Adam (view profile)

L = 40*membrane(1,25);
[rows cols] = size(L);
x = 1:rows;
y = 1:cols;

And voila! You can 3D print the Matlab logo! (or any other surface for that matter) =)

LEGAL NOTE: This may violate copyrights or trademarks. I am not liable if you choose to do this.

Thanks for the awesome code!

Comment only
14 May 2013 Algirdas Kluonius

Please help I get a flat gray surface, is it because my x ,y, are vectors and not meshgrid?

14 May 2013 junqua

junqua (view profile)

Really usefull for my research with flow over different surfaces

23 Dec 2012 Héctor Corte

Man great submision. I have used it to make some geek gif in my blog.

Comment only
23 Dec 2011 zaeazeae

Thanks for this file! You save me a lot of time! Very helpfull

24 Jun 2011 Jay Berger

nice work bill. i'm wondering about how one would go about adding labels to such stl files. such as, adding little spheres in a surface plot at a certain location (say x = 10, y = 10) and add labels such as "x = 10 & y = 10" at that location.

Comment only
13 May 2011 Samarth Raut

@ Prashanth,
AFAIK, .stl is inherently meant for triangulated geometry representation.

What you can do is to split rectangles into triangles - e.g. rectangle (n1, n2,n3,n4) gives two triangles - (n1,n2,n3) and (n3,n4,n1). Note that the order of n1,n2 etc is such that the normals obtained by cross product of vector, say n1_n2 x n2_n3 points outwards direction and there is consistency in this normal direction among all triagles. i.e. no sudden flips.

hope it helps,

Comment only
02 Nov 2010 Mahalingam Paheerathan

11 Jul 2010 Prashanth hanmaiahgari

Hi Bill,

What changes to be made in the program to get a stl with rectangular grid instead of triangular grid


18 May 2009 Zoltan

Zoltan (view profile)

But you could speed it up - it counts at large meshes, if you could find a way to avoid the for loop. For calculating the surface normals, you could use "surfnorm".

02 Mar 2009 Bart Ribbens

Great job! Thanx a lot!

28 Jan 2009 Daniel Lopes

Daniel Lopes (view profile)

Thanks Bill! You made a very useful MATLAB function for geometric modeling purposes.
Woks just fine!

19 Aug 2008 Bin Huang

Awesome! It helps me alot.

25 Jul 2008 Max Lingamfelter

Very nice program. Does anyone know a way to write the STL file while keeping other data intact to describe the color?

Comment only
20 Feb 2008 Yoel Shapiro

Very helpfull! I tried for a long time to open a MATLAB generated mesh or vrml in different 3D environments and couldn't. This did the job.

03 Oct 2007 Amanda L

very neat. Used for converting small pieces of scarcely digitized bone surface point clouds to STL surface which can be loaded into Mimics and Amira.

07 Mar 2006 Mike M

Has good nift. Use with gridgen to make mathematical surfaces into stl files for cfd. mmmm

Contact us