View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Converting a 3D logical array into an STL surface mesh

5.0 | 3 ratings Rate this file 27 Downloads (last 30 days) File Size: 74.1 KB File ID: #27733 Version: 1.6
image thumbnail

Converting a 3D logical array into an STL surface mesh


Adam A (view profile)


25 May 2010 (Updated )

Conversion of a voxelised object contained within a 3D logical array into an STL surface mesh.

| Watch this File

File Information

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.


This file inspired Export Voxel Data.

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
13 May 2016 Stefano Stablum

Hi Adam,
can you explain me how to compute "GridInput"?
I mean, I did a segmentation and now i have three coordinates vectors X Y Z. how can y use this program to have a Mesh?

Thank you!

Comment only
11 Mar 2015 Erez

Erez (view profile)

I have a logical array that is 2500 x 2500 x 2400. Will this run or am I wasting my time trying it on something so big?

Comment only
31 Oct 2014 Rik

Rik (view profile)

Nice program!
I am currently working on a 3d printer and I am looking for a function which can do the opposit, so converting a .STL or any other 3d cad model to a 3d logical matrix.
Does anyone know if there already exists such a function?

Kind regards,


08 Aug 2014 Nathan

Nathan (view profile)

Hi Adam,
Great program! Very helpful, and quite fast considering the computations involved.

20 Jan 2012 Adam A

Adam A (view profile)

Hi Johannes. If you're able to send an example of an array and the resulting mesh I will see if I can duplicate and fix any problems.

Comment only
17 Jan 2012 Johannes Korsawe

Really good and fast.
Sometimes there seem to be some artifacts (missing faces) in the resulting mesh. I have not already discovered where, but suspect a missing line somewhere.

Thanks for sharing!

27 May 2010 1.1

Improved memory usage.

29 Jun 2010 1.2

Improved speed.

30 Jun 2010 1.3

Major speed improvement

18 Aug 2010 1.4

Improved documentation

24 Nov 2010 1.5

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

09 Jul 2012 1.6

Now optionally output the mesh (Faces,Vertices) data

Contact us