How can I plot a 3D solid figure (not just 3d surface)
22 views (last 30 days)
Show older comments
I want to generate some solid models for 3D Printing. And the stl file is needed.
0 Comments
Answers (2)
KSSV
on 18 Nov 2016
1 Comment
DGM
on 28 Sep 2025
Edited: DGM
on 7 Oct 2025
Since R2018b, MATLAB has built-in STL tools
For legacy versions needing third-party tools, I'd recommend these tools over #22409. This explains why.
If you use #22409 or #20922 in a modern installation, you will shadow the inbuilt tools and likely cause problems for yourself unless you rename them.
David
on 14 Sep 2022
Edited: David
on 14 Sep 2022
I use surf2solid from https://www.mathworks.com/matlabcentral/fileexchange/42876-surf2solid-make-a-solid-volume-from-a-surface-for-3d-printing and Sven's stlwrite from https://www.mathworks.com/matlabcentral/fileexchange/20922-stlwrite-write-ascii-or-binary-stl-files -- not the Matlab 2018b stlwrite(). Then if I open the stl file in PrusaSlicer:

n = 30;
[X,Y] = meshgrid(linspace(0,1,2*n+1));
L = (40/51/0.9)*membrane(1,n);
figure, subplot(2,2,[1 3]), title 'Thin surface'
surf(X,Y,L,'EdgeColor','none'); colormap pink; axis image; camlight
subplot(2,2,2), title 'Block elevation'
[f,v] = surf2solid(X,Y,L,'elevation',min(L(:))-0.05); axis image; camlight; camlight
fv_block = struct('faces',f,'vertices',v);
subplot(2,2,4), title 'Thickness'
surf2solid(X,Y,L,'thickness',-0.1); axis image; camlight;
stlwriteSven('test.stl',fv_block)
1 Comment
DGM
on 30 Jul 2025
I know that's just mostly derived from the surf2solid() synopsis, but this usage does not strictly depend on the behavior of #20922. It can be done with the built-in stlwrite. The hazard in recommending #20922 is that most readers now already have an encoder by the same name and won't know that they will cause naming conflicts when they download it.
To clean the example up:
% some data
n = 30;
[X,Y] = meshgrid(linspace(0,1,2*n+1));
L = (40/51/0.9)*membrane(1,n);
% surf() resets the axes and deletes descendant objects,
% so the title can't be drawn first without extra work.
% at first i thought this was something that would have worked pre-R2013b,
% but no, it's just a mistake in the synopsis.
figure(1); surf(X,Y,L,'EdgeColor','none');
axis equal; camlight; title('Thin surface')
% lofting a solid block from a fixed elevation
figure(2); title('Block elevation')
[F V] = surf2solid(X,Y,L,'elevation',min(L(:))-0.05);
% write it
%stlWrite('test1.stl',F,V) % FEX #51200 or #20922 in R2018a or older
stlwrite(triangulation(F,V),'test1.stl') % in R2018b or newer
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','none');
view(3); camlight; axis equal; grid on
% normal offset by a given distance
figure(3); title('Thickness')
[F V] = surf2solid(X,Y,L,'thickness',-0.1);
% write it
%stlWrite('test2.stl',F,V) % FEX #51200 or #20922 in R2018a or older
stlwrite(triangulation(F,V),'test2.stl') % in R2018b or newer
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','none');
view(3); camlight; axis equal; grid on
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

