Is it possible to 3D print a 3D function plotted in Matlab?
11 views (last 30 days)
Show older comments
Greetings to all, I am trying to convert a 3D function plotted in Matlab (or convert it to STL file) to be able to print it in 3D, I am using the stlwrite command but it does not work, I attach my code, I appreciate any suggestions:
% Define your 3D function (example: parabolic function)
a = 1;
b = 2;
c = 3;
myFunction = @(x, y) a*x.^2 + b*y.^2 + c;
% Generate a mesh
x = linspace(-10, 10, 100);
y = linspace(-10, 10, 100);
[X, Y] = meshgrid(x, y);
Z = myFunction(X, Y);
% Triangulate the mesh
triangulation = delaunayTriangulation(X(:), Y(:), Z(:));
% Save the triangulated mesh as an STL file
filename = 'my_mesh.stl';
stlwrite(filename, triangulation.ConnectivityList, triangulation.Points);
1 Comment
Star Strider
on 12 Jul 2023
Please go ito a bit of detail about the problem. What does ‘it does not work’ mean? What is the result of using stlwrite?
Answers (4)
DGM
on 29 Jul 2025
Edited: DGM
on 29 Jul 2025
Neither stlwrite() nor FEX #20922 will accept tetrahedral meshes, and that's fine because you don't need a tetrahedral mesh for this anyway.
There are various ways to loft or offset simple surfaces like this, but this is an easy choice:
% Define your 3D function (example: parabolic function)
a = 1;
b = 2;
c = 3;
myFunction = @(x, y) a*x.^2 + b*y.^2 + c;
% Generate a mesh
x = linspace(-10, 10, 100);
y = linspace(-10, 10, 100);
[X, Y] = meshgrid(x, y);
Z = myFunction(X, Y);
% Triangulate the mesh
%[F V] = surf2patch(X,Y,Z,'triangles'); % just a thin, unprintable open surface
[F V] = surf2solid(X,Y,Z,'elevation',0); % a lofted surface (a solid)
% Save the triangulated mesh as an STL file
stlwrite(triangulation(F,V),'my_mesh.stl'); % you already have this
% display it
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','none');
view(3); view(-30,47); camlight;
I included two ways to triangulate the gridded data. If you use surf2patch() on an open surface, you end up with a zero-thickness object with zero volume. Maybe that's useful, but it's not printable. If you use surf2solid(), you can choose an offset method and either loft or extrude the open surface to form a closed surface representing a printable solid.
Even if your STL has nonzero volume, it's still up to you to make sure that the object is practically printable. Note the axes aspect in this plot. You have a tall, deeply concave part that is 20x20x300mm. Even with a flat base, that's going to be an unstable print, and heat control at the tips will be a problem (assuming FDM). It might not even fit on some printers. Other cases may require supports, vents, or other considerations.
0 Comments
Niranjan Sundararajan
on 12 Jul 2023
Not an official implementation of stlwrite but it works. Paste the function in your current working directory and rename it stlwrite. Then, you can call it by using:
stlwrite(filename, X, Y, Z);
It will work.
1 Comment
DGM
on 29 Jul 2025
This still just gives an unprintable, thin surface. OP already has an STL encoder. They don't need another, especially if it doesn't actually solve any part of the problem.
Cris LaPierre
on 12 Jul 2023
Edited: Cris LaPierre
on 12 Jul 2023
However, the input triangulation must be a triangulation object or 2-D delaunayTriangulation object. You are using a 3D delaunay triangulation.
Finally, there is no thickness to your object, which will prevent it from being able to be 3D printed.
3 Comments
Cris LaPierre
on 13 Jul 2023
They do not want to plot it. They want to save it to an STL file and 3D print it.
DGM
on 29 Jul 2025
Edited: DGM
on 29 Jul 2025
There isn't a MATLAB tool for writing tetrahedral meshes to an STL, probably because the description of an STL file only mentions triangular meshes. Similarly, FEX #20922 also does not support tetrahedral meshes.
It's not clear that OP's use of a tetrahedral mesh is intended or appropriate. Even if it were, your own answer does not make any use of the tetrahedral mesh either.
Cris LaPierre
on 13 Jul 2023
I would point you to this post: https://www.mathworks.com/matlabcentral/answers/623773-how-to-generate-a-stl-file-starting-from-a-3d-figure#answer_525658
This may be what @Niranjan Sundararajan is referring to. In that thread, the accepted answer suggests using the file exchange stlwrite function to create a 3D stl.
Here is code that works for me:
% Define your 3D function (example: parabolic function)
a = 1;
b = 2;
c = 3;
myFunction = @(x, y) a*x.^2 + b*y.^2 + c;
% Generate a mesh
x = linspace(-10, 10, 5);
y = linspace(-10, 10, 5);
[X, Y] = meshgrid(x, y);
Z = myFunction(X, Y);
% Triangulate the mesh
TR = delaunayTriangulation(X(:),Y(:),Z(:))
figure
tetramesh(TR)
% Save the triangulated mesh as an STL file
filename = 'my_mesh.stl';
stlwrite(filename,X,Y,Z);
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!