File Exchange

image thumbnail

Fast STL import function

version (2.16 KB) by Eric Trautmann
Function to import .stl geometry model files into matlab.


Updated 31 Mar 2011

View License

This version is currently ASCII only but future revisions will support binary. This function is based off of Luigi Giaccari's STL importer but uses the textscan function to dramatically increase speed, which makes a significant difference for large files.

Cite As

Eric Trautmann (2020). Fast STL import function (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (17)

Tao Linh


hello guys , I have a ASCII STL fie and i want to display it in matlab. but I dont know how to open it and to use all theses functions because I am beginner in Matlab. could you just tell me how to open it or where should i make changes. Thank in advance?

I am getting the following error:
Out of memory. The likely cause is an infinite recursion within the program.

Error in import_stl_fast (line 20)
[p,t,tnorm] = import_stl_fast(filename,1)

I am running Matlab R2017a academic, I removed the comment from lines 19 and 20 and changed the name of my stl file in line 19. Not sure where my mistake is. Any help is much appreciated.

Matt Carney


I am using Matlab 2013 maybe that's causing all the issues but I replaced "if (C{4}(end)== NAN)" to "if(isnan(C{4}))"
Works great now.
Let me know if that solved the problem.
Cheers !

Did anyone find a solution to the "if (C{4}(end)== NAN)" Error. I tried using isnan funtion as well as suggested still didn't work!
Any updates/help will be appreciated.
Thank you

I have the same problems: removed all'\r' in line 47. I get two errors:
>import_stl_fast(filename, 1)
>Error using import_stl_fast (line 31)
>invalid input number /mode setting
>> import_stl_fast(filename, 2)
Subscript indices must either be real positive integers or logicals.

Error in import_stl_fast (line 61)
if (C{4}(end)== NAN)


Once I changed line 47 it works great! Super fast!

Solution: in line 47 'fmt = ...' remove all the '\r'. Those might be needed in Windows?

Hello Dear,

You have written a very useful code for stl files.

However, i have one question.

How to display the output after reading an stl file. as i tried patch to display it but it is not displaying the correct model which i gave as input.

can you tell me how to display the output of your function? or do we need any conversion before that?

I too am getting this error. Anyone have a solution?


@Pierrick Bersier: I'm getting the same error.

When I try to load my STL with that script, I get every time the same error. an sombody help ?

Subscript indices must either be real positive integers or logicals.

Error in import_stl_fast (line 59)
if (C{4}(end)== NaN)

Works OK for ASCII STL files. Thanks.

If any current users are interested in a binary stl import, please let me know.

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