File Exchange

image thumbnail

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 09 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.

Comments and Ratings (20)

Md. Rahman

Hi thank you very much for this highly useful program. However, I was wondering if the program works for non-uniform grid (e.g. gridZ with unequal spacing between the values)? Thank you.

Xinzhuo Hu

Peng Jiang

Thanks! Very useful!

wang zihao

this should help lost people:
gridX, gridY, gridZ are the coordinates in phisical space. I use Dicom data, my voxel size is 0.7, 0.7 and 1. so my 200x200x200 is in real life a 140mm x 140mm x 200mm. my gridX/gridY should be a vector that goes in steps of 0.7 0.7, 1.4, 2.1, 2,8...140 (mm) and my gridZ is 1 2 3 4 5 6 7 -200mm


Great job on converting voxels to stl file. It do help me a lot. Thank you so much.

Vujih Phoen

Percy Fan

John Natale

Hello and thank you for sharing!
But I don't understand what is gridX, gridY, gridZ ?

Thank you!

Hello Adam. Thank you for sharing. I'm having a dificulty... How do I determine the values of the gridX, gridY and gridZ arrays?
Thank you.


I have a question, if I want to convert a .mat file in an .stl file, how can I use your program, because I have problems with this part [faces,vertices] = CONVERT_voxels_to_stl('temp.stl',gridINPUT,gridX,gridY,gridZ,'ascii');

What are you doing there?
Someone knows?


I used this function to convert a stack of masked MRI images into a number of very useful 3D STL models, and I have to say that it works like a charm! Many thanks for sharing this with the community, it will continue to be a useful tool in the future.

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!


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?


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,



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

Adam A

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.

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!


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.

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

Inspired: Export Voxel Data