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.

Comments and Ratings (14)

Matt Carney

Viren Mistry


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 !

Viren Mistry

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

Brian Guthrie

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)


Carlos (view profile)

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?

Andrew Hundt

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


Fritz (view profile)

@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)

Ilkay Meseli

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

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