How to convert 3D plot to .STL

40 views (last 30 days)
승준 이
승준 이 on 17 Oct 2021
Commented: DGM on 14 Jul 2025
I wonder how to export an implicit 3D function to a .STL file for 3D printing.
I have searched for some tools like
but cannot figure out how to do it. The following code is the 3D plot that I want to convert to .STL. Thank you in advance.
h=fimplicit3(@kidney2);
set(h, 'EdgeColor', 'none', 'MeshDensity', 125);
view(-110,80)
axis([-0.1, 0.1, -0.1, 0.1, 0, 0.5])
function out=kidney2(x,y,z)
kidney1=@(x,y,z) (x.^2+y.^2).^(2)-(x.^3+y.^3).*(z/4-z.^2).^(1/2);
dt=0.25*(z/4-z.^2).^(1/2);
out=kidney1(x+dt,y+dt,z);
end

Answers (2)

Raghava S N
Raghava S N on 21 Feb 2024
Hello,
A possible solution is to download the “stlwrite.m” file from the following link: https://www.mathworks.com/matlabcentral/fileexchange/20922-stlwrite-write-ascii-or-binary-stl-files
This is prescribed by a MathWorks staff member in the following MATLAB Answer - https://www.mathworks.com/matlabcentral/answers/623773-how-to-generate-a-stl-file-starting-from-a-3d-figure#:~:text=You%20need%20to%20download%20the%20stlwrite.m%20file%20from%20the%20following%20link%3A.You can refer to the same MATLAB Answer, where they also describe some intricacies of “stlwrite”.
  2 Comments
Dyuman Joshi
Dyuman Joshi on 21 Feb 2024
These solutions won't work because there is no data available corresponding to implicit surface generated.
DGM
DGM on 14 Jul 2025
If you'd read that other thread, you'd understand that the problem was caused by the user writing code based on an old third party encoder instead of the encoder that's already in MATLAB. The answer in that case was obvious. Either get the tool you're trying to use, or write your code to suit the tools you already have. It not a concept specific to the topic of writing an STL, and the answer isn't relevant to this question.
FEX #20922 does have some features that the built-in stlwrite() doesn't have, but they're not needed for this question. Like @Dyuman Joshi said, none of these encoders will help us if we have nothing to write.
There are reasons to recommend third party tools, but they're not common anymore. If you don't know or can't even mention what they might be, then you're just contributing to the problem that you unwittingly showcased. The reason that the user in #623773 was blindly writing code for a function that they didn't know they didn't even have was because they found some old code somewhere and just used it. It's just as bad to mislead users into downloading tools that they don't really need (or that don't work), so that they unknowingly shadow the tools that they already have. Either way, you wind up with people saddled with errors, confused that functions don't match the documentation.

Sign in to comment.


DGM
DGM on 14 Jul 2025
Here.
% what we're given
hfs = fimplicit3(@kidney2);
set(hfs, 'EdgeColor', 'none', 'MeshDensity', 125);
view(-110,80)
axis([-0.1, 0.1, -0.1, 0.1, 0, 0.5])
% get the data from the fimplicit object
F = hfs.Triangulation.ConnectivityList;
V = hfs.Triangulation.Points;
% write it to an STL
stlwrite(triangulation(F,V),'test1.stl') % R2018b+
Okay. There. We're done.
But wait. Before anyone jumps out of the bushes and suggests that you need to go download some incomplete decoder from the File Exchange or GitHub or the dumpster behind Taco Bell, no, you don't. Use the tools you already have.
% read it back just for show and tell
T = stlread('test1.stl');
[F V] = t2fv(T);
figure
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','none');
view(3); view(177,61); camlight;
axis([-0.1, 0.1, -0.1, 0.1, 0, 0.5]); grid on
% OP's function
function out=kidney2(x,y,z)
kidney1 = @(x,y,z) (x.^2+y.^2).^(2)-(x.^3+y.^3).*(z/4-z.^2).^(1/2);
dt = 0.25*(z/4-z.^2).^(1/2);
out = kidney1(x+dt,y+dt,z);
end
Why does it look slightly wrinkly? I think fimplicit() and fsurf() do some sort of mesh smoothing during display, so you don't really get to see the triangulated surface as flat faces. That information isn't in the FV data, and an STL file doesn't store any such information. Consider it ephemeral graphical glamour. It's been dispelled, but maybe if you have some other renderer that does smoothing, you can load the model and relive the illusion.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!