Hey guys, my stlwrite function is not working and i would like to know how to fix it. In the code C is random points
6 views (last 30 days)
Show older comments
seeds=C;
[Ver,Cel,C_tst]=voronoi3d_cuboid(seeds,Vcub);
FK = zeros(100,3,312);
figure
hold on
axis('equal')
view([-36 27])
scatter3(seeds(:,1),seeds(:,2),seeds(:,3),25, ...
'Marker','o','MarkerFaceColor',[1 0 0], 'MarkerEdgeColor','k');
scatter3(Ver(:,1),Ver(:,2),Ver(:,3),25, ...
'Marker','o','MarkerFaceColor',[0 1 0], 'MarkerEdgeColor','r');
for k = 1:length(Cel)
if ~isempty(Cel{k})
col=rand(1,3);
Vk = Ver(Cel{k},:); Fk = convhull(Vk);
if exist('mergeCoplanarFaces.m','file')==2
[Vk, Fk] = mergeCoplanarFaces(Vk, Fk);
for i=1:length(Fk)
patch('Vertices',Vk,'Faces',Fk{i},'FaceColor',col,'FaceAlpha',0.3)
end
else
trisurf(Fk,Vk(:,1),Vk(:,2),Vk(:,3),'FaceColor',col, ...
'FaceAlpha', 1,'EdgeAlpha',1,'EdgeColor','k');
%
FK(1:size(Fk,1),1:3,k) = Fk(:,1:3);
VK(1:size(Vk,1),1:3,k) = Vk(:,1:3);
end
end
end
grid on
xlabel('X');ylabel('Y');zlabel('Z');
P = struct('faces',FK,'vertices',VK);
stlwrite('mytriangulation.stl',P);

1 Comment
Answers (1)
VINAYAK LUHA
on 6 Oct 2023
Hi Akhshay,
It is my understanding that you are facing issues while using the “stlwrite” function. The issue is caused by the incorrect placement of the passed parameters in the calling syntax for the "stlwrite" function.
Refer to the examples listed in the following documentation of “stlwrite” function to learn how to use “stlwrite” function correctly- https://in.mathworks.com/help/matlab/ref/stlwrite.html
Hope this helps in fixing the “stlwrite” error.
Regards,
Vinayak Luha
2 Comments
Rik
on 6 Oct 2023
Is this a practice post?
The OP didn't respond in a year and a half. Why resurrect it now?
DGM
on 5 Apr 2025 at 16:12
OP was not using the MATLAB version of stlwrite(). They were using this:
That said, neither tool would have worked without extra work, the goal of which would have required clarification. After figuring out how to even test the code, it throws an error because the inputs aren't valid sizes. For a set of (npoints = 50) random points in a unit cube, FK is 100x3x312, and 95% of its rows are invalid zero entries -- partly because OP allocated it for this fixed size instead of having it sized as needed. VK is 34x3x50, and is also largely filled with redundant zero vertices. This stuff shouldn't have even been crammed into a monolithic ND array in the first place.
Appropriately, neither tool will accept ND arrays of mostly invalid or redundant geometry. Disregarding the preallocated size of FK, it only has 50 (in this case) pages with any content.
Each page of FK and VK describe a closed surface (one of the cells). You'd have to get rid of all the invalid zero entries from each page of FK and write each page of FK,VK individually. Alternatively, you could cram them all in one STL, but you'd have to remap all the triangles and merge the lists, and in the end, you'd have an STL that someone would probably have problems with because all the objects are tangential, and who knows what we're trying to do with this file(s).
See Also
Categories
Find more on Voronoi Diagram in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!