Converting matlab file to stl file

13 views (last 30 days)
Anh Phan Viet
Anh Phan Viet on 16 Mar 2020
Answered: DGM on 17 Jul 2025
Hello everyone, Can anyone please help me convert matlab file to file stl. My plot 3D consists of 7 sets of [X,Y,Z]. I've tried function surf2stl, it requires X,Y,Z. Therefore I concatenated 7 [X1 X2 ... X7] = X Y and Z also. But the graph doesnt look like the one I need.

Answers (2)

Kevin Chng
Kevin Chng on 17 Mar 2020
  1 Comment
Anh Phan Viet
Anh Phan Viet on 17 Mar 2020
Edited: Anh Phan Viet on 17 Mar 2020
Sorry but I've not succeeded. Maybe the reason is that: I have [X1 Y1 Z1], [X2 Y2 Z2], ..., [X7 Y7 Z7] while this can only accept 1 set [X Y Z]. However, when I combined 7 sets of coordinates to 1 set, I've met some errors with my stl file.

Sign in to comment.


DGM
DGM on 17 Jul 2025
For example:
% you have multiple objects defined by gridded xyz data
[x1,y1,z1] = peaks(20);
[x2,y2,z2] = sphere(20);
z2 = z2 + 6;
% plot them if you want
hs(1) = surf(x1,y1,z1); hold on
hs(2) = surf(x2,y2,z2);
axis equal
% convert the gridded data to triangulated F,V data
% surf2patch() can accept surf handles or
% it can also accept the XYZ arrays directly
for k = 1:numel(hs)
% gridded XYZ to F,V lists
[thisF thisV] = surf2patch(hs(k),'triangles');
% concatenate the F,V lists
if k == 1
F = thisF;
V = thisV;
else
F = [F; thisF + size(V,1)]; %#ok<*AGROW>
V = [V; thisV];
end
end
% write the file
T = triangulation(F,V);
stlwrite(T,'test.stl')
% read it back just to show the result
T = stlread('test.stl');
trisurf(T,'facecolor',[1 1 1]*0.8,'edgecolor','none')
view(3); view(-30,33); camlight;
axis equal; grid on

Community Treasure Hunt

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

Start Hunting!