File Exchange

image thumbnail


version 4.0.1 (92 KB) by Sunil Bhandari
slice stl files and create continuous contour along the slices


Updated 10 Aug 2018

View License

This contains the matlab files to slice a stl file and create a continuous contour along which the machine can move for deposition for 3D printing.
The main file is slice_stl_create.m
The supporting functions are triangle_plane_intersection.m, read_binary_stl_file.m, orient_stl.m, rotate_stl.m and plot_slices.m.
The script stl_slice_and_plot.m is an example using the functions.

Comments and Ratings (46)


I have updated the file so that the first move is from point with lower x-coordinate to higher x-coordinate or from point with lower y-coordinate to higher y-coordinate. This should solve the problem you described.


It is my implementation binary search algorithm. I find the triangles that need to be considered for each slice using this step. Because of this, I don't need to consider all the triangles in the model and only the ones that are intersecting one particular plane.

Hi Sunil,
I am trying to understand your code and am not able to understand part of finding intersecting traingles related to determining high and low in zslices. Please can you tell me related to this...

jian liu

Hi Sunil:
I noticed that after slicing, the inner loop of a layer countour is sometimes in the counterclockwise instead of the correct clockwise direction.
The attached STL is the example of a simple Ring. If I slice it with a slice_height of 0.04mm, the first 14 layers are correct, i.e., the outer loop part of movelist_all (output of slice_stl_create_path ) is counterclockwise and the inner loop part is clockwise . However, begining at 15th layer, the inner loop is also counterclockwise.

Christina F.

graph is an in-built function in Matlab since 2015b.
You could follow the link below for reference.

Ashish Mehta

Hi Sunil
Is the graph function in slice_stl_create_path.m missing? Please help me understand what graph does.
Can you please share me the files to

thanooja rao

Hello sir,
can I have coding on adaptive slicing method....where the slicing thickness is called by the algorithm...
I need code as a part of my assignment...
plz....sir Can you drop in my mail box
Thank you in advance....

Gerald Lee

I have known the {movelidt}. Thank you!

Gerald Lee

I have some difficulty in data structure of the cell {movelist} . Are there the coordinate values of "double vertexes of each intersect line" in {movelidt} ? Kind of you to give me some advice !
I am going to make ues of them to reshaping the 2D graph and generating the G-code.

-1e-5 and +1e-5 are there to ensure the lowest and the highest z values in the stl model are accounted for. 1e-5 is an arbitrary small number. Any other small number should work fine.

Hello i have a question regarding the stl_slice_create_path function. What is the reason for the terms -1e-5 & +1e-5 at the end of the min/max function?

the 60 x 14 matrix is made by 60 triangles with 3 vertices and one normal vector. If the P1(x1,y1,z1), P2(x2,y2,z2), and P3(x3,y3,z3) are the three vertices and N(n1,n2,n3) is the normal vector for a triangle. The first row of the triangles matrix that represents this triangle will be:
x1, y1, z1, x2, y2, z2, x3, y3, z3, n1, n2, n3

If you are using ASCII stl file instead of binary stl file, you will need to read the data and arrange the data in this format.

Gerald Lee

I found that triangles is a matrix with 60X14 dimension. it is different with the stl file datatype that have coordinates of 3 points and a normal vector of each triangle. I want to use the stl file(with coordinates of 3 points and a normal vector of each triangle) to update the program that always shows exceeding the dimension and cannot test another stl model .Could you give me some advice?

renqiang tian

Sunil Bhandari:I want to that iterates through an array of every Triangle object and finds the minimum and maximum slope of the entire Triangle array. The slope is equal to the angle of a Triangle's normal vector and the Z axis (either the positive or negative direction, whichever is the smaller angle). Can you realize my idea? Thank you.

renqiang tian


I updated the files so that you can orient the stl file along x, y or z axis before slicing.

Vijay Anand

In matlab, is it possible to rotate the stl model?
Kindly help me out. I need to generate slices in one of the three axis depending on requirement.

you could always rotate the input STL model to orient it in the X-direction or the Y-direction.

Vijay Anand

A Very Nice Submission.
Can the code be extended to slice in X or Y directions as well ?
It will be highly appreciated.
Thank You in advance.

Noah Bellomo


The "triangles" variable is an array with coordinates of points (3 points with three x y z coordinates each) and a normal vector (with x y z coordinates) to the surface formed by the triangle. This gives 12 columns for each row, where each row gives the coordinates for one triangle.

The cell array given out after the slicer program has been run is the list of coordinates through which the extruder head has to move and make depositions. Each cell in the cell array is the movement path for a given layer height.

In additive manufacturing, depositions start from the bottom and deposition is made in the direction opposite to the normal of the triangle. For subtractive manufacturing, you will have to start from the last layer and remove material in the direction of the normal of the triangle.

Noah Bellomo

Could you explain more about the spread sheet of values under the "triangles" variable, after the slicer program has been run on an stl. what do the vectors mean? We are trying to take this information and run it in an array so that it can go where the slice is not. (subtractive manufacturing based on an STL)

Dongmin Han

The code is not commented very well but it is not difficult to figure out.
p1, p2 and p3 are the vertices of the triangles. t1, t2 and t3 are just some ratios in z-direction which will be used to calculate the coordinates of the intersections. And obviously, intersect1, 2 and 3 are intersections of three edges and the slicing plane.
i1, 2, and 3 determine if each intersection is on the edges of the triangles (they could be out of the segments).
Hope this helps.

Dongmin Han

thank you so much for sharing the code! it worked well.

renqiang tian

Sunil Bhandari :
Can you explain these meanings to me? please, about :p1,t1,intersect1,i1,and imain,
I've been studying them for a long time and I don't know how to get them.
Do you have any information about this?
If yes, can you send me a copy?
thank you... my


uniquetol was first introduced in Matlab R2015a. Are you sure you have a Matlab version R2015a or higher?

Luo zeyi

Undefined function 'uniquetol' for input arguments of type 'double'.

Hi, I try to run the code but I received following error, would you pls help me?

Error in slice_stl_create_path (line 137)
nodes = uniquetol(nodes,tol_uniquetol,'ByRows',true);

Error in stl_slice_and_plot (line 9)
tic;[movelist, z_slices] = slice_stl_create_path(triangles, slice_height);toc;

renqiang tian

Sunil Bhandari :very thank you,I have resolved the issue,
but,I don t know what is meaning in the “new_line_plane_intersection”function,example:p1,p2,p1,c = ones(1,size(p1,2))*z_slices,t1 = (c-sum(P.*p1))./sum(P.*(p1-p2)),imain = i1+i2+i3 == 2;,pts_out。
I can't understand its. Could you explain it to me,please?


looks like the stl file you are using is too big.
Each row in "triangles" is one triangle with three vertices and one normal. Each of three vertices is represented using three doubles. The normal is also represented using three doubles. That is why there are 12 columns per row.

renqiang tian

thanks:Sunil Bhandari
Error using zeros
The requested 537529712x12 (48.1GB) array exceeds the default maximum array size

Out of memory. Type HELP MEMORY for your options.
Error in read_binary_stl_file (line 7)
triangles = zeros(numTriangles,12);

Error in stl_slice_and_plot (line 2)
triangles = read_binary_stl_file('zfx.stl');

why did it have to happen? What does "12" mean?

for the error listed below, the error is there because you changed 'f' to 'C' in 'rd = fread(C,inf,'uint8=>uint8')'. Also, you need to provide the path for your own file in the line 'triangles = read_binary_stl_file('C:\Users\sunil.bhandari\Downloads\skullashtray1.stl');'

Undefined function or variable 'C'.
Error in read_binary_stl_file (line 5)
rd = fread(C,inf,'uint8=>uint8');
Error in stl_slice_and_plot (line 7)
triangles = read_binary_stl_file('C:\Users\sunil.bhandari\Downloads\skullashtray1.stl');
why?Can you explain it to me?

triangles = read_binary_stl_file(filename)
Index exceeds matrix dimensions.

Error in read_binary_stl_file (line 7)
numTriangles = typecast(rd(81:84),'uint32')

why?thank you

Hello, do you have any papers on this subject? If yes, can I get a connection?

Warning: Input arguments must be scalar.
> In read_binary_stl_file at 6
In stl_slice_and_plot at 3
Error using reshape
Size arguments must be integer scalars.

Error in read_binary_stl_file (line 7)
sh = reshape(rd(85:end),50,numTriangles);%每个三角面片占50个字节,

Error in stl_slice_and_plot (line 3)
triangles = read_binary_stl_file('qiu.stl');

why?can you help me ?

hamid reza

Hello,Sunil Bhandari,triangles = zeros(numTriangles,12);
I did not have the foggiest idea what it meant.Can you explain it for me? Thanks

renqiang tian


出错 slice_stl_create_path (line 14)
min_z = min([triangles(:,3); triangles(:,6);triangles(:,9)])-1e-5;

function [movelist_all,z_slices] = slice_stl_create_path(triangles,slice_height)

错误: 此上下文中不允许函数定义。

why?Can you help me?

renqiang tian

Undefined function or variable 'C'.

Error in read_binary_stl_file (line 5)
rd = fread(C,inf,'uint8=>uint8');

Error in stl_slice_and_plot (line 7)
triangles = read_binary_stl_file('C:\Users\sunil.bhandari\Downloads\skullashtray1.stl');

why?Can you explain it to me?

the problem is that if the STL file has triangles that have not been formed properly, or if there are gaps, an extra piece of code needs to run, which I commented out in the files for the sake of speed. I will uncomment that in the new upload.

The getting stuck thing was a bug in the step where I group the triangles by layer heights. I noticed that bug earlier and corrected it. But I could not find time to upload it. I will upload the corrected code.


Also.... when putting the control slice height to 0.2 the function "slice_stl_create_path" gets stuck in an infinite loop in i=1621
Do you have any idea why this might be?

Hey! Very good work.
I'm having issues reading this file (!Am0SDZmYuu4dhLEOREo71yavTzbqgA).... do you have any idea what might it be?
The issue is theat the first couple of layers at 0.3 slice height doesnt get done correctly

Shalom, sorry about that. I wrote triangle_plane_intersection to replace new_line_plane_intersection. But I missed replacing it in the code. You can replace "new_line_plane_intersection" with "triangle_plane_intersection" and the code should work fine. I will also upload the corrected solution.

Thank you for pointing out the mistake.


Shalom (view profile)

I get a "Undefined function or variable 'new_line_plane_intersection'." every time I run the code. Can you upload this missing function?



error corrections to the gui


gui tool added

corrected files uploaded for version

updated so that the sliced paths move in same direction when possible

function to read ASCII stl files added

Function to rotate stl file added.

The stl file can now be oriented along x, y or z axis before slicing. The original configuration is assumed to be oriented along x axis.

function to orient the stl file along x,y or z axes added

bug with infinite loop corrected
code for improperly formed triangles uncommented

Corrected code:
"new_line_plane_intersection" replaced with "triangle_plane_intersection"

STL fileread speed increased. Large stl files can be read in seconds.

intersecting triangles with each slicing plane now calculated using binary search. Big O for this step changed from O(nk) to O(nlogk), where n is the number of triangles and k is the number of slicing planes.

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

MATLAB Online Live Editor Challenge

View the winning live scripts from faculty and students who participated in the recent challenge.

Learn more

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

» Watch video