How do you export a plot into a stl file?

11 Ansichten (letzte 30 Tage)
Sophia Smith
Sophia Smith am 18 Mär. 2018
Kommentiert: DGM am 19 Jul. 2025
t = [0:0.1:2*pi] a = sin(t); plot(t,a)
Trying to export this sin plot as an stl. file. I may need to make it 3d first. Any ideas?

Antworten (2)

voxey
voxey am 14 Dez. 2018
DT = delaunayTriangulation(xc,yc,z);
sha=alphaShape(xc,yc,z);
[T,Xb] = freeBoundary(DT);
TR = triangulation(T,Xb);
stlwrite(TR,'yourstlname.stl',text)
  1 Kommentar
DGM
DGM am 19 Jul. 2025
I'm going to assume this was supposed to be an answer and not just a wordless, unformatted question-as-answer.
Two things are easy to see:
This line obviously does nothing. It's not used for anything, and it's not clear why it would be used for anything.
%sha=alphaShape(xc,yc,z); % this does nothing
... but both these lines share the same issue. Your usage implies OP has z data. What exactly are these inputs supposed to be? Since we're not even using alphashape() for anything, we don't really need to figure out what the consequences might be of feeding it various things, but what about delaunayTriangulation()? What are we feeding it?
%DT = delaunayTriangulation(xc,yc,z) % what does this mean in context?
Let's assume we just have the original 2D curve data at some z-level (e.g. z = 0). If we explicitly provide some constant z-data, we're asking for a 3D (tetrahedral) mesh of 2D data.
% the 2D curve
npoints = 50;
xc = linspace(0,2*pi,npoints).';
yc = sin(xc);
z = zeros(npoints,1);
% if we assume that the curve is strictly 2D,
% then we're creating an empty tetrahedral mesh,
% since all the tetrahedra are degenerate.
% (the convex hull of a set of coplanar points has zero volume)
DT = delaunayTriangulation(xc,yc,z) % empty mesh
DT =
delaunayTriangulation with properties: Points: [50×3 double] ConnectivityList: [] Constraints: []
So that gives us nothing. What if we simply omit the unnecessary constant-valued z-data and just use the syntax which is suited for the 2D data that we actually have?
% the 2D curve
npoints = 50;
xc = linspace(0,2*pi,npoints).';
yc = sin(xc);
z = zeros(npoints,1);
% if we try to simply do a 2D triangulation for the 2D curve,
% what we get is the convex hull in 2D.
% all we have is an open curve with no area,
% so constrained triangulation isn't an option.
DT = delaunayTriangulation(xc,yc);
% it's the convex hull in 2D
patch('faces',DT.ConnectivityList,'vertices',DT.Points, ...
'facecolor','w','edgecolor','k');
axis equal; grid on
That gives us the convex hull in 2D, which is not likely what was intended. OP didn't clearly describe what they wanted, but they did want it in 3D. What if we provide an offset curve so that our z-data is not constant-valued?
% the 2D curve
npoints = 50;
xc = linspace(0,2*pi,npoints);
yc = sin(xc);
h = 1; % offset height
Vxy = [xc; yc].';
V = [Vxy h*ones(npoints,1); Vxy zeros(npoints,1)];
% if we have two edge curves describing the boundary
% of a surface in 3D, then we get the tetrahedral
% mesh of the convex hull in 3D.
DT = delaunayTriangulation(V);
% then we get the outer surface of the convex hull
[T,Xb] = freeBoundary(DT);
% it's the same thing as before, but extruded into 3D
figure
patch('faces',T,'vertices',Xb,'facecolor','w','edgecolor','k');
view(3); view(-30,47); camlight;
axis equal; grid on
Again, we just get the convex hull.

Melden Sie sich an, um zu kommentieren.


DGM
DGM am 19 Jul. 2025
It's not really clear what the goal is. You have a line with zero thickness in 2D, and you want it to be "3D" somehow. I'm going to make the simplest assumption and assume that the goal is to extrude the curve in the z-direction to create a 2D surface living in 3D.
% the 2D curve
npoints = 50;
t = linspace(0,2*pi,npoints); % use linspace if you want the whole interval
a = sin(t);
plot(t,a)
% create the vertex list using an offset copy
h = 1; % offset height
Vxy = [t; a].';
V = [Vxy h*ones(npoints,1); Vxy zeros(npoints,1)];
% create wall triangles
nverts = size(V,1)/2;
va = (1:npoints).'; % top
vb = va + nverts; % bottom
vc = circshift(va,-1); % top
vd = vc + nverts; % bottom
F = [va vb vc; vd vc vb];
F([nverts end],:) = []; % get rid of closure faces
% given a curve that progresses in the +x direction,
% the above will produce triangles with face normals in the -y direction
% if you want to flip the face normal direction, just flip F
F = flip(F,2); % now face normals are in +y direction
% plot it
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','k');
view(3); view(-30,47); camlight;
axis equal; grid on
% at the time, MATLAB did not have a built-in STL encoder
%stlWrite('test.stl',F,V) % FEX #51200
% but since R2018b, it does
stlwrite(triangulation(F,V),'test.stl')
Again, this is a surface. It has no thickness, so it's unclear what the appropriate winding order is. In other words, since it encloses no volume, there is no "inside". Likewise, the face normals can't point "outward", since there is no "outward". We just have to guess how OP expected them to be oriented.
If the goal is instead to give the line some thickness and extrude it into a solid model, that's a different story.

Community Treasure Hunt

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

Start Hunting!

Translated by