Converting a 3D logical array into an STL surface mesh

Version (74.1 KB) by Adam A
Conversion of a voxelised object contained within a 3D logical array into an STL surface mesh.
Updated 9 Jul 2012

View License

Converting a 3D logical array into an STL surface mesh

Adam H. Aitkenhead
The Christie NHS Foundation Trust
24th May 2010


>> CONVERT_voxels_to_stl(STLname,gridDATA,gridX,gridY,gridZ,'ascii')
>> [faces,vertices] = CONVERT_voxels_to_stl(STLname,gridDATA,gridX,gridY,gridZ,STLformat)

converts the voxelised object contained within the 3D logical array <gridINPUT> into an STL surface mesh, which is saved to the ascii file <STLin>. The x,y,z axes for <gridINPUT> are defined by <gridX>, <gridY> and <gridZ>. The (faces,vertices) data are optional outputs.


STLname - string - Filename of the STL file.
gridINPUT - 3D logical array of size (P,Q,R) - The voxelised object (1 => Inside the object, 0 => Outside the object)
gridX - A 1xP array - List of the X axis coordinates.
gridY - A 1xQ array - List of the Y axis coordinates.
gridZ - A 1xR array - List of the Z axis coordinates.
STLformat - string (optional) - STL file format: 'binary' or 'ascii'.


faces - Nx3 array - A list of the vertices used in each facet of the mesh, identified using the row number in the array vertices.
vertices - Nx3 array - A list of the x,y,z coordinates of each vertex in the mesh.


For an example, run the following script:
>> CONVERT_voxels_to_stl_example


- This code does not apply any smoothing. The stl mesh will be exactly the same geometry as the original voxelised object.

Cite As

Adam A (2024). Converting a 3D logical array into an STL surface mesh (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Export Voxel Data

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

Now optionally output the mesh (Faces,Vertices) data

Allow the STL to be written in either binary or ascii format.

Improved documentation

Major speed improvement

Improved speed.

Improved memory usage.