i'm doing a simulation in matlab which uses a 3D matrix as input, i.e. the matrix has the form (xDim, yDim, zDim). With a simple STL export and a voxelization of this STL file i can get only a matrix with all elements be either one or zero. But my part has four different materials which i need to distinguish in matlab. One single slice in the matrix should look like:

0 0 0 0 1 1 1 1 2 2 2 0 ...
0 0 0 1 1 1 1 2 2 2 0 0 ...
0 0 0 1 1 1 1 1 2 2 2 2 ...

As a first step: in which format should i export my part to preserve the materials as some value?

  • $\begingroup$ Can you export the four materials as separate STLs, modify the 1's in the other materials as you wish, and combine the matrices? $\endgroup$ Jun 20, 2018 at 10:49
  • $\begingroup$ STEP AP214 preserves colours upon export, which I use for differentiating between components when importing models into programs for rendering, but I don't have experience with then converting that to a voxel cloud $\endgroup$ Jun 20, 2018 at 10:50
  • $\begingroup$ Did you distinguish materials in the part using split bodies? $\endgroup$
    – dROOOze
    Jun 20, 2018 at 11:19
  • $\begingroup$ Using four different exports was my first thought, too. But is is a bit cumbersome when editing the part. I will have a look at STEPAP214, thanks. I have four separate bodies with four different materials applied. $\endgroup$ Jun 20, 2018 at 12:05

1 Answer 1


If you are stuck with using STL, here's a crude way to do it:

enter image description here


  • Create two references which extend beyond the object in the x, y, z space (I used the cylinders shown on the upper right and lower left corners)

  • For each material (body), export STL along with the references

  • Voxelise using the same parameters

The references are used to ensure that the binary voxel arrays are generated in a consistent space between the different bodies.

You can multiply each separate voxel array using a different number (2,3,4 ...) and then add them together. If you want to get rid of the references, you can use Matlab's bwlabel function to detect unconnected regions and remove them.

  • $\begingroup$ May i ask, how you voxelize the STLs? At the moment I use a python script in between SolidWorks and matlab. $\endgroup$ Jun 20, 2018 at 12:23
  • $\begingroup$ @MarvinNoll I just Googled it and took the first result that came up. $\endgroup$
    – dROOOze
    Jun 20, 2018 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.