getting this error with stlwrite

5 views (last 30 days)
P Kamal Kumar
P Kamal Kumar on 30 Nov 2019
Edited: DGM on 2 Jul 2025
code:
model = createpde;
importGeometry(model,'ag455.stl');
pdegplot( model,'FaceLabels',"on");
u = generateMesh(model)
stlwrite(u,'mesh.stl','binary')
Error using stlwrite (line 25)
Input argument must be a triangulation object.
  2 Comments
Adam Danz
Adam Danz on 17 Feb 2020
See the examples in the stlwrite documentation.
Note how the inputs are constructed.

Sign in to comment.

Answers (2)

Rami Ali Al-Khulaidi
Rami Ali Al-Khulaidi on 7 Oct 2021
To save it using stlwrite, I should get triangulation from stlread.....
u=stlread('ag455.stl');
stlwrite(u,'mesh.stl','binary')

DGM
DGM on 2 Jul 2025
Edited: DGM on 2 Jul 2025
I think @Rami Ali Al-Khulaidi is about right in an oblique way. If you want the object geometry, you already have it. You don't need to save anything.
However, if you want an STL containing the (I presume) tetrahedral mesh generated by generateMesh(), then you're out of luck. The input to the built-in stlwrite() must be a triangulation object. While triangulation() supports both triangular and tetrahedral meshes, stlwrite() only supports plain triangular meshes.
If your model is 3D, what you have is a quadratic tetrahedral mesh represented by a 10xT list. The triangulation class does not support such a thing. As far as I know, there are no STL writers or readers which will support such a thing. I might be missing one, but most STL tools I've seen on the FEX presume that meshes are triangular. A cursory glance at references suggests that that's the specification.
If you were to try to reduce the model to a 4xT tetrahedral mesh using the 'linear' option, you could generate a triangulation object, but you're still not going to write it with stlwrite().
unzip stepholecube.stl.zip
model = createpde;
importGeometry(model,'stepholecube.stl');
pdegplot( model,'FaceLabels',"on");
%u = generateMesh(model) % default is 'quadratic'; tetrahedron list is 10xT
u = generateMesh(model,'geometricorder','linear') % list is 4xT
u =
FEMesh with properties: Nodes: [3×3040 double] Elements: [4×13433 double] MaxElementSize: 0.0693 MinElementSize: 0.0346 MeshGradation: 1.5000 GeometricOrder: 'linear'
T = triangulation(u.Elements.',u.Nodes.'); % supports Tx3 or Tx4
stlwrite(T,'mesh.stl','binary') % supports only _triangular_ meshes (Tx3)
Error using stlwrite (line 33)
Tetrahedron triangulation is not supported.
If there were an STL writer that can support a quadratic tetrahedral representation of a mesh, are there any external applications that can read such a file? If the answer is "no", then there's no point creating such a file. Save it as a .mat file or something else.

Tags

Community Treasure Hunt

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

Start Hunting!