# Reading Matlab mat file into 3D Slicer

**URL:** <https://discourse.slicer.org/t/reading-matlab-mat-file-into-3d-slicer/1987>\
**Category:** Support\
**Tags:** matlab\
**Created:** [January 31, 2018, 3:51pm UTC](https://discourse.slicer.org/t/reading-matlab-mat-file-into-3d-slicer/1987 "2018-01-31T15:51:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marasov](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@marasov](https://discourse.slicer.org/u/marasov)\
**Post date:** [January 31, 2018, 3:51pm UTC](https://discourse.slicer.org/t/reading-matlab-mat-file-into-3d-slicer/1987/1 "2018-01-31T15:51:11Z")

</div>

Hello!  
I made several X-ray images of a metal ball of different density from different angles.  
Then I applied CBCT reconstruction to them and got a \*.mat file.  
[3D Cone beam CT (CBCT) projection backprojection FDK, iterative reconstruction Matlab examples](https://www.mathworks.com/matlabcentral/fileexchange/35548-3d-cone-beam-ct--cbct--projection-backprojection-fdk--iterative-reconstruction-matlab-examples)  
How can I transfer the received data to a 3D Slicer to render the object and get a 3D model?  
Thank you all in advance!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 31, 2018, 4:13pm UTC](https://discourse.slicer.org/t/reading-matlab-mat-file-into-3d-slicer/1987/2 "2018-01-31T16:13:14Z")

</div>

Probably the easiest is to save the reconstructed image as a standard NRRD image file.

There is a Matlab writer for NRRD image files here:

> <https://github.com/PerkLab/SlicerMatlabBridge/blob/master/MatlabCommander/commandserver/nrrdwrite.m>

If you want to run Matlab code directly from Slicer, then you can use the [MatlabBridge extension](https://www.slicer.org/wiki/Documentation/Nightly/Extensions/MatlabBridge).

---

<div class="post-metadata">

**Author:** ![alireza](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alireza/32/67173_2.png) [@alireza](https://discourse.slicer.org/u/alireza)\
**Post date:** [August 9, 2018, 7:39pm UTC](https://discourse.slicer.org/t/reading-matlab-mat-file-into-3d-slicer/1987/3 "2018-08-09T19:39:45Z")

</div>

Thank you Andras,  
Is it possible to give pixel spacing information too? Where should I put that information?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 9, 2018, 10:15pm UTC](https://discourse.slicer.org/t/reading-matlab-mat-file-into-3d-slicer/1987/4 "2018-08-09T22:15:52Z")

</div>

`img.ijkToLpsTransform` transform specifies IJK voxel coordinates to LPS anatomical coordinates transform. Pixel spacing is the norm of the columns of the top-left 3x3 submatrix. If volume is axis-aligned then spacing values are in the diagonal of the top-left 3x3 submatrix.

---

<div class="post-metadata">

**Author:** ![Chicago\_Girl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chicago_girl/32/14816_2.png) [@Chicago\_Girl](https://discourse.slicer.org/u/Chicago_Girl)\
**Post date:** [April 23, 2022, 11:54pm UTC](https://discourse.slicer.org/t/reading-matlab-mat-file-into-3d-slicer/1987/5 "2022-04-23T23:54:48Z")

</div>

I have a 2D matrix and I wanna make it to nrrd but this function doesnt work.  
The reason that I want to change it to nrrd is I want enforce the module that I’ve written on Slicer on this matrix.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 24, 2022, 3:51am UTC](https://discourse.slicer.org/t/reading-matlab-mat-file-into-3d-slicer/1987/6 "2022-04-24T03:51:51Z")

</div>

I’ve just tested the [nrrwrite.m script](https://github.com/PerkLab/SlicerMatlabBridge/blob/master/MatlabCommander/commandserver/nrrdwrite.m) and it works perfectly for 2D images. For example:

```matlab
a=eye(30)
img.pixelData=a
nrrdwrite("c:/tmp/test.nrrd", img)

```

Creates an image like this:

```txt
NRRD0005
# Complete NRRD file format specification at:
# http://teem.sourceforge.net/nrrd/format.html
type: double
space: left-posterior-superior
dimension: 3 
sizes: 30 30 1
space directions: (1,0,0) (0,1,0) (0,0,1)
kinds: domain domain domain
endian: little
encoding: raw
...

```

It gets loaded into Slicer correctly:

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/4/649a7e3eb8d05f3be6d1a6feeeff2633c5a7e44f.png)

Note that others may have created scripts called `nrrdwrite.m` and `nrrdread.m` and those scripts may or may not work correctly. Make sure you use the scripts that we developed and made available [in the Slicer Matlab Bridge repository](https://github.com/PerkLab/SlicerMatlabBridge/blob/master/MatlabCommander/commandserver/).
