This function is used for loading binary STL files.
There are already two stlread.m files available on the mathworks file exchange site. Harlev's version reads in the data and returns matrices with X/Y/Z formatted vertex data. Johnson's program reads in the data and returns Face/Vertex/Normal matrices.
This submission also exports Face/Vertex/Normal matrices, but runs about 30 times faster than Johnson's version and 13 times faster than Harlev's version (which doesn't export face normal data).
This submission will also properly read in color values (if present) and the STL file title.
For more info (including timing info) see: http://www.esmonde-white.com/home/diversions/matlab-program-for-loading-stl-files
Hello, thank you for this great function!
I was able to run the function and read an STL Binary file. The function returns the information about vertices and so on. Can this function generate the file in STL ASCII format? Or as Vincent explained here, this function does not convert the binary into ASCII? Please reply. Thanks!
I was albe to solve the problem myself...
I saved the files as STL ASCII Format. That could not be converted by your function.
When i saved them as STL Binary Format everything worked fine.
Hey @Franzis (or anyone else who can help),
I'm trying to use your function. I used the one from Eric Johnson before and it worked. Now both yours and his dont work on my stl files. I reworked some data with stlwrite (https://de.mathworks.com/matlabcentral/fileexchange/20922-stlwrite-filename--varargin-) ,that had over 600.000 points. On Autodesk Meshmixer i reshaped the data and reduced the vertices.Now i have 38679 vertices and 74618 triangles. Then i exported the stl file. But when i tried to use your programm this error pops up:
Error using repmat
Requested 48x808792373 (289.2GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long
time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
Error in stlread (line 72)
ind = reshape(repmat(50*(0:(numFaces-1)),[48,1]),[1,48*numFaces])+repmat(trilist,[1,numFaces]);
Glad for help
Works great for me? I especially found the demo file helpful to help me figure out how to view and light my stl after importing it.
Thank you very much for this great function. I tested it with a huge file (1.6 millions triangles), it is fast and works well.
I don't need color so I didn't test it.
I wrote a function for reading STL ascii if it is of interest to someone.
Dear author,I want to know that how I can use it,I'm a matlab beginner
Great script just cant get it to generate colors
It's fast, no joke. Read a 400 MB file with 8M triangles for me in 6 s.
Ron Perrone - enter the filename as a string.
Unfortunately I couldn't get the script to run. I have a binary stl file in my MATLAB folder, but when I put the name of the file in the function it just says undefined function or variable... any suggestions?
Unfortunately it doesn't run for me. Says it requires more input arguments, not just the stl filename. Bummer. Any suggestions?
Thank you very much for sharing! Greetings to Georgia :-)
Memory expensive, the script quickly crash because the computer is out of memory. The script extract about two billion face (numFaces) from my 80Mb STL files. From my calculation, I would need about 100 Gb of ram to import my file......
Does someone have a solution for this problem?
Hi. thanks for this program. can you send me flowchart?
A very good job to read STL binary files. Thanks.
This stl file reader loads binary STL files. I am unfamiliar with Catia, or the type of STL files exported by Catia. If you do not have a binary STL file, you should try one of the ASCII STL file readers available on the File Exchange.
Have you read the help for this program? When you run this program, it will output the faces, vertices and normals from the STL model.
I also find it odd that you rated the program as three stars, but do not seem to have used it at all.
I have a problem . I have saved a catia figure in stl format
now i want to read its vertices and normals in MATlab
can you please help me with the function .. this is related to my final year project.
I have a problem with importing an STL graphics file to the Simulink diagram. I've tried making the model both in UGS NX and SolidWorks but when I import it, the dimensions are multiplied a 1000 times.
I think it's because Simulink's default lenght unit is 'm' but every CAD software writes it in 'mm'. As I look-up into the binary file, there's no real information about the units in the STL file so my question is: how do I change the default unit in Matlab or how can I convert the STL file itself to match my requirements.
You can definitely change the patch face (or patch edge) colors, and even specify transparency. For more information, I suggest you read the mathworks/matlab documentation for the patch command. For example, type 'doc patch' at the command line, and look at the section under 'Coloring Patches'.
Thank you for your file which helped a lot in the project. I just don't understand that when I run the file to open my stl file ,the plot is right but the color is totally black.Is that a mesh?Can I change the color with patch('Faces',f,'Vertices',v,'r');?
The STLVertexNormals submission is now available at http://www.mathworks.com/matlabcentral/fileexchange/33226-compute-stl-vertex-normals
I have submitted a new file to the Mathworks file exchange which calculates patch normals using a triangle-area based smoothing method. It will take a while to be approved by the mathworks, so I'll email it to you in the meantime. I thought that this was an interesting exercise, so I created my own function called 'STLVertexNormals'. The compiled 'Patch Normals' submission you linked to is almost certainly faster (I haven't played around with it), but the code I have submitted is (hopefully) quite readable.
First off thank you very much for taking the time to post this function it is very easy to use and does exactly what it says. I just have one question, the function returns a normal matrix and going by the size of it I assume it contains the face normals. But Matlab uses the Vertex normals when doing lighting calculation for a patch. Apparently it can calculate these normals itself for performing these calculations but this does not seem to be working for me. So I was wondering if you may know how to translate these face normals into vertex normals? so I can force Matlab to use them for lighting.
I have looked into adapting this function
because he seems to calculate the face normals first then uses them to calculate the vertex normals like i would like to do but have not had any success with it to date.
If you have any ideas that may help me this this it would be very appreciated.
Either way, thanks again for sharing this function.
The problem is that you have an acsii STL, not a binary STL. The binary file reader assumes that the first few bytes describe the number of faces. In ascii STL files, the first few bytes will be text instead, and define some arbitrarily large numbers. If you can export to binary files instead, they will be *much* smaller and load more quickly.
If you open the file with wordpad, you can see the ascii text. Try this m-file instead: http://people.sc.fsu.edu/~jburkardt/m_src/stla_io/stla_io.html
I have more information about STL files on my personal web site: http://www.esmonde-white.com/home/diversions/matlab-program-for-loading-stl-files
Please let me know if I can do anything else to help!
I have 1.7022e+009 faces for 55mb stl file and same problem for the 1mb stl file and the code found 1.9193e+009 faces (var numFaces). is it normal?
A 55 MB binary STL file is quite large. Each triangle face of your model requires 50 bytes. Since your larger file is 55 MB I'll estimate the number of faces as 1.15 million triangles based on (55*1024*1024)/50.
Line 72 of the program will require about 3 copies of the 1.15 million triangle matrix (with each byte inefficiently stored as 8 bytes- the matlab double precision format). This will take at least 1.32 GB of memory. In addition, you will need another matrix of the same size for the actual data, requiring a total of 1760 MB (I'm probably underestimating), which must be exceeding your system memory limit (what are the specs of the computer are you trying to run this on?). Could you make a slightly lower resolution model, or break it into pieces?
I can also play some tricks to reduce the memory footprint of the program if necessary, such as reducing the numerical precision of the intermediate matrices and reading the data from the file only after creating the "ind" matrix. Some of these changes will slow the program down, others will pose limitations on the total number of triangle faces that can be handled, so making these changes are not ideal.
Have you tried loading the smaller model? There should be no issues loading a 1 MB stl file, even on an antiquated old PC.
thanks for submitting this m-file. I encountered one problem: i have two stl file (55mb and 1mb) and matlab says
"Maximum variable size allowed by the program is exceeded.
Error in ==> stlread at 72
is it possible to solve this problems?
Thanks and your help is appreciated in advance.
1. Lee White described using patch objects on this page, his comment is immediately before your first comment. For more information, look at:
2. If you have an assembly, then you should also have files for each individual part forming the assembly. You can create STL files from each part separately.
3. You can load the STL files for each individual part, then use HGtransform to assemble the parts virtually. This will be more difficult than using the assembly tools directly in solidworks. If you create the STL from the solidworks assembly, there is no straightforward way to re-associate the triangles with the original parts.
I am very thankful for your replay. I am still confuse about the following points you made above:
1. Where did Lee White describe Parenting "patch objects to hgtransform objects to manipulate patches built from stls" as I do not know what he means by (hgtransform).
2.If I have assembly part contains from several parts , I am not able to create individual stl file for each part since the whole part is one piece as you know.
3.Do you have any thought how I could associate triangles with original parts that come from?
Thanks and your help is appreciated in advance.
Unfortunately, if the model is a single STL file, it has probably been turned into a single patch object (a single closed surface described by triangles). While you could probably determine a way of associating certain triangles with the original parts that they come from, you won't have a description of the whole part. There is no way that you will be able to recover exact Solidworks models from the STL files. If you want to load the STL models into Matlab from Solidworks, you should export each part as an STL file, then load them all into matlab as different STL models, and transform them as described by Lee White above. Also, the STL models will be imperfect representations of the parts (curved surfaces will be approximated by a tesselated surface).
I hope that is of some help.
I am interesting in subdivide the stl file to patches in which each patch represent subpart that build an object. I am doing assembly technique where assembly part contain several parts and I draw them in SolidWorks. From my knowledge, that STL file is unorder file so that I lost tracking in stl file which data are related to which subpart. When I copy some of the stl file output and redraw them using SolidWork, I will get some parts which are not what my original parts were. I am highly appreciate your help in this issue and how can I get ordering stl file so it will reflect my exact data representation on SolidWork.
Works great. Liana, check out parenting the patch objects to hgtransform objects to manipulate patches built from stls. Each hgtranform object has a position and orientation defined by a transformation matrix. You can create serial mechanisms this way like robotics arms. The hgtransform objects are parented to each other in a chain. Moving a parent objects moves all children with it.
The STLread program doesn't move the model at all, and there is no functionality for rotating the 3D matrix included in this code. A 3D rotation can be performed by multiplying all the vertices by an appropriate rotation matrix. See http://en.wikipedia.org/wiki/Rotation_matrix#Rotations_in_three_dimensions for more information about rotation matrices.
If I have time, I will write and post a program for 3D model rotation.
Hi Francis! I downloaded the file to Matlab directory and renamed my stl file to MyModel.stl.
At the first line of the code, I changed stlread(filename,verbose) to stlread('MyModel.stl'). I've tried all sorts of thing but I keep getting this error,
??? Error: File: stlread.m Line: 1 Column: 43
Unexpected MATLAB expression.
Thank you for this function, it's very helpful. Could you please tell me how can I change the orientation of the loaded model according to the XYZ zero point using outputs of your function? Let's say, I want that a polyhedral object (simple house model) loaded from STL file would lay on the XY plane instead of the ZX plane. Thanks!
I'm not quite sure why you are interested in breaking the object up into multiple segments. Is it that you want to have a composite object where different parts can move relative to each other? If so, just treat each subsection as an independent object. I would be happy to help, please send me an email with more information about the part you're trying to load (firstname.lastname@example.org).
Also, please include more information about exactly how you're using the file loader. From the error message, it's possible that the STL file isn't quite right (perhaps it is an ascii stl file or it doesn't conform to standards) or that you're using the program incorrectly.
Hello. I am interested in dividing a stl.file into different segments (defined by vertices, so that I can load individual parts of an object at a time. I would like to be able to save each part of the object separately and have the ability to load them together as an assembly to form the whole object. Would this function be helpful? I tried it out with a simple part I made with Unigraphics and when I tried to display the model with patch(...), I got an error meesage saying
??? Error using ==> patch
Faces values must be >= 1.0
Vertices array must have at least 2 and at most 3 columns (or be empty)
FaceVertexCData must be an Mx1 vector (indexed colors) or Mx3 matrix (RGB colors)
Do you know what my issue may be? The file was saved as a binary stl.
You should not need to change any code in the stlread.m file.
For more information on using 'stlread', download the stlread.m file, and copy it into your matlab working directory (the directory shown in the address bar at the top of your matlab window). Next, type 'help stlread', and it will display the program help information.
To use the program, use the following command to load your model.
[v, f, n, c, stltitle] = stlread('MyModel.stl');
This will read the model into the v and f matrices. To display the model, try starting with:
One important consideration, is whether or not your STL file is a binary or ascii STL file. If you try to open your STL model with a text editor (like wordpad), if you can see a clear list of numbers, then you have an ascii STL file. The above program only works for binary STL files. If you need an ascii STL reader, try looking at http://www.mathworks.com/matlabcentral/fileexchange/24233-stlimport-import-function-for-stl-files
thank you for submitting this m-file. could you guide me on how to read my stl file. i am a beginner in matlab. do i need to change some lines in your program? thank you again.
Win cash prizes and have your live script featured on our websiteLearn more
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.