# Loading transformation from Matlab .mat file

**URL:** https://discourse.slicer.org/t/loading-transformation-from-matlab-mat-file/2226
**Category:** Support
**Created:** [March 2, 2018, 11:37am UTC](https://discourse.slicer.org/t/loading-transformation-from-matlab-mat-file/2226 "2018-03-02T11:37:30Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Vinny](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vinny/32/7052_2.png) [@Vinny](https://discourse.slicer.org/u/Vinny)
#### Post date: [March 2, 2018, 11:37am UTC](https://discourse.slicer.org/t/loading-transformation-from-matlab-mat-file/2226/1 "2018-03-02T11:37:30Z")

</div>

Hi,

I tried to load a FSL generated .mat transformation matrix as a transform but the system crashed. The simple goal is to use the .mat matrix to transform vtk objects into a different imaging space; using 3d Slicer, I envision dragging the vtk objects onto the transform node and hardening the transformation and saving the transformed vtk objects.

Thanks for your help.

---

<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: [March 2, 2018, 12:32pm UTC](https://discourse.slicer.org/t/loading-transformation-from-matlab-mat-file/2226/2 "2018-03-02T12:32:31Z")

</div>

Instead of saving transform in proprietary Matlab `mat` file format, could you save the transform into ITK transform file format? Slicer can directly read/write ITK transform files.

You can use Slicer’s MatlabBridge extension to run Matlab functions directly from Slicer. See more information here: [https://www.slicer.org/wiki/Documentation/Nightly/Extensions/MatlabBridge](https://www.slicer.org/wiki/Documentation/Nightly/Extensions/MatlabBridge)

You can also use this Matlab function that writes a linear transformation matrix to an ITK transformation file: [https://github.com/PerkLab/SlicerMatlabBridge/blob/master/MatlabCommander/commandserver/cli\_lineartransformwrite.m](https://github.com/PerkLab/SlicerMatlabBridge/blob/master/MatlabCommander/commandserver/cli_lineartransformwrite.m)

---

<div class="post-metadata">

### Author: ![Vinny](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vinny/32/7052_2.png) [@Vinny](https://discourse.slicer.org/u/Vinny)
#### Post date: [March 2, 2018, 5:55pm UTC](https://discourse.slicer.org/t/loading-transformation-from-matlab-mat-file/2226/3 "2018-03-02T17:55:22Z")

</div>

Thanks Andras. I believe ITK stores the matrix in LPS format but from what I see in the cli\_lineartransformationwrite.m function, there is the option to save transformation matrix in RAS. My images are in RAS so I’m guessing that I’d have to set the transformType to ‘slicer’?

---

<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: [March 2, 2018, 6:15pm UTC](https://discourse.slicer.org/t/loading-transformation-from-matlab-mat-file/2226/4 "2018-03-02T18:15:15Z")

</div>

ITK transform is always stored as LPS. The flag controls if you work in RAS or LPS, so that it converts to/from LPS transforms files accordingly.

---

<div class="post-metadata">

### Author: ![Vinny](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vinny/32/7052_2.png) [@Vinny](https://discourse.slicer.org/u/Vinny)
#### Post date: [March 3, 2018, 5:01pm UTC](https://discourse.slicer.org/t/loading-transformation-from-matlab-mat-file/2226/5 "2018-03-03T17:01:40Z")

</div>

Hi,

I tried to load the FSL generated .mat transformation matrix into Matlab and got the following error message:

```
>> a = load('affine_ct_mr.mat')
Error using load
Unable to read MAT-file C:\Users\Vinit\Documents\MATLAB\affine_ct_mr.mat. Not a binary MAT-file. Try load -ASCII to read as text.

```

So, I had converted the .mat array into a .txt file using FSL and was able to read this into Matlab as a 4x4 array:

```
>> b = load('affine_ct_mr.txt')

b =

    0.9892 0.0481 0.1165 -13.8786
   -0.0145 0.9642 -0.2878 13.6338
   -0.1213 0.2732 0.9450 12.4001
         0 0 0 1.0000

```

When I attempt to run the cli\_lineartransformwrite.m function, I get the following error message:

```
>> cli_lineartransformwrite('affine_ct_mr_slicer', b, 'slicer')
Undefined function or variable 'cli_transformwrite'.

Error in cli_lineartransformwrite (line 31)
cli_transformwrite(outputfilename, allTransforms);
```

---

<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: [March 3, 2018, 7:10pm UTC](https://discourse.slicer.org/t/loading-transformation-from-matlab-mat-file/2226/6 "2018-03-03T19:10:27Z")

</div>

> [@Vinny](#):
>
> Undefined function or variable ‘cli\_transformwrite’.

Matlab requires all necessary .m files to be in the current working directory or in the path. Either change the working directory to `.../MatlabCommander/commandserver` or add this folder to the path in Matlab.

As it seems that you are very new to Matlab, I would advise to spend minimum necessary time with it, and instead focus your efforts on learning Python, which is a much more versatile language, with many more libraries, much larger community, and it is completely open-source and free.

---

<div class="post-metadata">

### Author: ![Vinny](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vinny/32/7052_2.png) [@Vinny](https://discourse.slicer.org/u/Vinny)
#### Post date: [March 3, 2018, 11:40pm UTC](https://discourse.slicer.org/t/loading-transformation-from-matlab-mat-file/2226/7 "2018-03-03T23:40:15Z")

</div>

Actually, I’m modifying my workflow to include doing intermodality registrations directly in 3d Slicer based on our convos. This way transformations can be directly calculated without requiring an intermediary Matlab step. I fully agree with what you are saying about Python and open-source. I’ve been doing my neuroimaging work in Nipy precisely for the reasons of versatility and support; the very same reasons why I’m using 3d Slicer. Appreciate all your help.
