Converting matlab file to stl file
13 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
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
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!