# Writing to STL files 17

Posted by **Jiro Doke**,

Jiro's pick this week is `surf2stl` by Bill McDonald.

About a month ago, at a seminar I gave at a university, a graduate student came up to me and asked whether there was a function for writing out 3D model files. Specifically, he was referring to STL files (stereolithography files). It was a topical question because it came soon after I talked about some of the exporting capabilities in MATLAB for graphical file formats.

I knew that we didn't have any built in, but I promptly suggested checking out the File Exchange. I've seen a few functions for reading from STL files, so I figured there would be counterparts. Sure enough, there were a few. Here's a list of entries with a tag of "stl".

From the list, I am highlighting Bill's `surf2stl`. It has nice help, clean code, good reviews, and has even inspired another entry.

Download and give it a try!

Get the MATLAB code

Published with MATLAB® 7.8

**Category:**- Picks

## 17 CommentsOldest to Newest

**1**of 17

I have the following question and hope that you may shed some light on it. Thanks in advance

I am working on image, how to recover scale, rotation, shear from projective t-matrix?

For example:

1.6971 1.838 0

-1.314 0.989 0

0 0 1

**2**of 17

Yousif, I’m not sure I understand your question. It’s probably better to post your question on the newsgroup

**3**of 17

How,

I have download the code,

but I really don’t know what is x,y,z mean in the file.

My question is, I have a 3D matrix, and how can I use surf2stl() to export a STL file?

Thanks for answering!

**4**of 17

Ro,

X, Y, Z corresponds to the inputs used by SURF. 3D matrix would not work with SURF, only 2D matrices.

**5**of 17

Thanks for answering,

So, do you know anu way to convert 3D matrix to STL file?

**6**of 17

Ro,

I can’t really answer that because I don’t know what your 3D matrix represents. First step is to get your matrix in a form that can be visualized using the command SURF. For that, I suggest reading the documentation for SURF. Once that’s done, then you can use surf2stl function highlighted in this blog.

**7**of 17

jiro,

My 3D matrix represents many points. I have tried the command Surf. But it only works with the 2D matrix. For example, the 3D matrix represents many points in (x,y,z) format. The command Surf can work the same ‘z’ coordinate, so I can’t figure out how to transfer to 3D STL.

**8**of 17

Ro,

I’m sorry but I don’t know how to solve your problem. In order to use surf2stl, you have to represent your data in x,y,z format — the same way it is used in surf(x,y,z). Since your 3D matrix does not work with surf, you have to first convert it to something that does.

One question, what is the SIZE of your 3D matrix. Is it something like m-by-n-by-o ?

I suggest either posting your question in the newsgroup or contacting support.

**9**of 17

jiro,

I think I can figure out something.

So the question now is: Asuming I have three points, there coordinates are (x1,y1,z1),(x2,y2,z2)and(x3,y3,z3).

How can I use the command surf2stl() to export a stl file?

Thanks for answering me again!

**10**of 17

jiro,

I have solved the problem by the command patch2stl.

Thanks for your suggestion!

**11**of 17

Ro,

I’m glad it worked. It seems like your data was structured more for PATCH, not SURF.

**12**of 17

Hey jiro,

did you write the patch2stl code yourself?

I can’t find anything like that in the other blogs.

You would really help me if you could publish your patch2stl code.

Cheers

Madeleine

**13**of 17

@Madeleine,

“patch2stl” is written by someone else and is available on File Exchange:

http://www.mathworks.com/matlabcentral/fileexchange/20922-patch2stl

**14**of 17

Hey jiro,

did you write the patch2stl code yourself?

You would really help me if you could publish your patch2stl code.

:)

**15**of 17

hey, hello

could you help me, plese?

i want to know your STL Maltal code

because, I’m studying on making coronal image by using 3d image :P

**16**of 17

@Jo Byung Doo

See my response #13. I’ve included the link to the File Exchange entry.

**17**of 17

Dear, could you help me reading stl file in matlab?

thanx alot

Iam drawing a part by AutoCAD and I need to read the part stl file using matlab code, so if you have like that code, please send it to me, and thanx for your help

## Recent Comments