File Exchange

image thumbnail

STL File Reader

version (1.6 MB) by Eric Johnson
STLREAD imports geometry from a binary stereolithography (STL) file into MATLAB.


Updated 20 Jul 2011

View Version History

View License

In addition to the STLREAD import function, this submission also includes a small demo that loads an STL model of a human femur bone.

FV = STLREAD(FILENAME) imports triangular faces from the binary STL file
indicated by FILENAME, and returns the patch struct FV, with fields 'faces'
and 'vertices'.

[F,V] = STLREAD(FILENAME) returns the faces F and vertices V separately.

[F,V,N] = STLREAD(FILENAME) also returns the face normal vectors.

The faces and vertices are arranged in the format used by the PATCH plot

Cite As

Eric Johnson (2021). STL File Reader (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (87)

pongkorn aumthuk

Syed Muhammad Hamza

Mohamad Bakhaidar

I am having difficulty running the demo
Error: File: stlread.m Line: 1 Column: 30
Invalid expression. Check for missing multiplication operator, missing or unbalanced
delimiters, or other syntax error. To construct matrices, use brackets instead of

Error in stldemo (line 15)

any idea what could be the issue??


This is a great function. Thank you so much!

Ishan Srivastava

Great work. Thank you!

Jesús Lucio

Perfect. Fast and does what is expected.

Johan Wickström


hank lee

Austin Fite

Mike smith

great function, thanks. It helps a lot.

Michael McGeehan

Thanks! Is it possible to apply a scaling factor to the .stl file once it has been read in?

Arda Aksu


Hii how to do a 3d plot with the stl file after importing

Oleg Iupikov

@lu wen, This depends on the STL file. If you would like to fix it, just execute
[fv.vertices, ~, ic] = uniquetol(fv.vertices, 1e-6, 'ByRows',true);
fv.faces = ic(fv.faces);

The submission is very useful, thanks to the author for sharing!

lu wen

the variable fv.faces always be [1,2,3; 4,5,6; 7,8,9; ...]. Points of the same coordinates are recognized multiple times.

lu wen

Pavlo KOchkin

harsh jain

Matthew Ward


thank you it works fine! but as it has been mentioned in a previous comment I believe it gives wrong faces, i.e the faces don't seem to have any node in common.



Bashar Alsadik

Lukas Hulinsky

victor bic

Great, tks!
How could I plot a field (colors) onto the Stl rendered surface?

Xiaowei Zhou

Great. Easy to use.

Patrick Shorey


Hi, I'm currently working with STL file, I try to use your program to read STL files but I got some issues.
It works fine with your example but if I want to read an other STL, I got an error coming from NaN you use in Stp binary. It says 'Out of memory'. Can you help me fix it ?

Very easy to read .stl files.

A question, how to load the .stl file into matlab, and save it as a matlab file?


Jay Willis

Excellent thanks.
Rotate the patch with rotate(h, [x,y,z], degrees) where [x,y,z] is direction e.g.[1,0,0] etc.
Translate with h.YData = h.YData+ translation;
and scale...I have yet to discover

Carlos Duque

Stephen Sebastyan

works fine for binary STLs, not so much for ASCII. Gives a memory overflow error.

Miguel Gilabert

Works perfectly


Tomer Gur Apter

Roche de Guzman

Dayna Grimshaw

I've had the same problem as Max Hardwick-Morris, does anyone have a solution to this problem please? Had a variety of error messages from tweaking the code and the folders but it's always the fv = stlread('femur.STL') line that produces an error. Any help would be greatly appreciated!

Alexander Laut

function almost crashed my computer when I tried loading a STL (ASCII). Works fine with binary as far as I can tell.

Luca Bartolomei

Masi maisot

thanks dude!

Karoline Neumann

Works great. Thank you very much

Li Zheng

Sorted, use rotate3d on, not rotate on.

Li Zheng

It's good. Any functions to rotate the view? When I try to turn rotate on, it comes error that 'h must contain axes children only'


Thank you! That's really useful.

Jean Gautier

Hi Eric, I'm currently working with STL file, I try to use your program to read STL files but I got some issues.
It works fine with your example but if I want to read an other STL, I got an error coming from NaN you use in Stp binary. Can you help me fix it ?

Di Zhu

hao li

thank you

Fadi Kahwash

Works well for me. Thanks Eric!!


Max Hardwick-Morris

Hi Eric,

When I run this demo I get an error message "Undefined function or variable 'stlread'."

Is there a toolbox I may be missing to run this stlread function?

Thank you in advance!

Andrea Menichetti

Thank you! It worked for me at first attempt!

André André

André André

Steffen Ossmann


Brandon Jones

I have been attempting to convert CAD files to MATLAB for the past week. I have gone through a half dozen xml and iges toolboxes and none worked. This program worked the first time I tried it and has an easy to understand interface. Thank you.

Victoria Hung

thank you so much! that's really useful and impressive!

Iñigo Moreno

@Evan That is because the vertices appear one time for each face, try running:

[v, ~, index] = unique(v, 'rows');
f = index(f);

Ron Perrone

Does anyone have any suggestions for any other files that can do this? Can't seem to get this one to work. The demo didn't work either. Looks good, but need one that is a little more user friendly. Any suggestions are greatly appreciated. Im sure this one is 5 stars for those who can get it to work.

Evan Terrell

Great function, but one problem. When I open my stl in a mesh editor, the surface has 55,000 vertices. However, when I read the file into Matlab with this script, there are nearly 330,000 vertices. Any idea what the problem is?

Ron Perrone

Unfortunately this script wouldn't run for me. Keeps saying more input requirements are needed to run, even though I have the correct name of the file.

wayclif jordan


Di Zhu

works well

hao gu

deeply impressed

j p

Excellent!! but only support binary format.

Brad Saund

Omar Osama

Michael Volk

How do I get this to work?


Worked like a charm on a very complex SE-translated STL file.

Todd Brintlinger

This function is for binary STLs only, and will cause problems when used with ascii STL. The conditional statement against reading ascii STLs is commented out.


what is meant by faces and vertices?

I want to generate a 3D-array out of my STL-files.. is there a way?

Memory expensive, the script quickly crash because the computer is out of memory. The script extract about a billion points from my STL files. From my calculation, I would need about 100 Gb of ram......

Does someone have a solution for this problem?


Hi. thanks for this program. can you send me flowchart?
Best regards.


Great work!

Christian Kotz

Great! fast and easy to use.

Héctor Corte

Wonderful submision. I used it to make some geek gif in my blog.


Very helpful

Santosh ARS

i think this gives wrong faces? Please confirm.


Thanks for your submittal. What units is the model in once I've read the stl file with Matlab. I am using NextEngine's 3D scanner and they are telling me that the units are defined by the reader utility.

Morteza Shahpari

Morteza Shahpari

Very nice and useful.

I have re-written this function to be much faster.


By ?

very good

Luigi Giaccari

Very useful for binary filesl but do not support ascii format


very useful!

can the vertices and faces be arranged such that they can be used with surf??

textures cannot be applied to patch objects. surf is more useful.

alain boyer

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

Community Treasure Hunt

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

Start Hunting!