# Extensions/MatlabBridge: Send transform (4x4 matrix) from Matlab to 3D Slicer

**URL:** https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159
**Category:** Support
**Created:** [June 12, 2018, 7:48pm UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159 "2018-06-12T19:48:15Z")
**Posts on this page:** 11
**Page:** 2

<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: [June 15, 2018, 5:29am UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/21 "2018-06-15T05:29:59Z")

</div>

Probably the simplest is to revert all your changes in the .xml and .m file and just modify these lines in your .m file:

```
img.pixelData = some3DArray;
img.ijkToLpsTransform = [0.05 0 0 0; 0 0.05 0 0; 0 0 0.05 0; 0 0 0 1];
```

---

<div class="post-metadata">

### Author: ![katharina\_hecker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/katharina_hecker/32/3431_2.png) [@katharina\_hecker](https://discourse.slicer.org/u/katharina_hecker)
#### Post date: [June 15, 2018, 5:33am UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/22 "2018-06-15T05:33:49Z")

</div>

thank you very much.  
so just to understand it correctly: I use not theis part anymore:

```
img=cli_imageread(inputParams.inputvolume);

outputParams.min=min(min(min(img.pixelData)));
outputParams.max=max(max(max(img.pixelData)));

img.pixelData=(double(img.pixelData)>inputParams.threshold)*100;

cli_imagewrite(inputParams.outputvolume, img);

```

instead I just add:

```
img.pixelData = some3DArray;
img.ijkToLpsTransform = [0.05 0 0 0; 0 0.05 0 0; 0 0 0.05 0; 0 0 0 1];

```

and leave the XML file like it is.

I really appreciate your effort. it is so important for me

---

<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: [June 15, 2018, 5:37am UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/23 "2018-06-15T05:37:22Z")

</div>

`cli_imageread` reads data from Slicer. If your image is taken from a different source then this part is not needed.

`cli_imagewrite` writes the volume so that Slicer can read it, so that is still needed.

You have to take care of replacing `some3DArray` with actual data that you get from somewhere.

---

<div class="post-metadata">

### Author: ![katharina\_hecker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/katharina_hecker/32/3431_2.png) [@katharina\_hecker](https://discourse.slicer.org/u/katharina_hecker)
#### Post date: [June 15, 2018, 5:38am UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/24 "2018-06-15T05:38:27Z")

</div>

ok thanks I will try it out.  
The xml-file i dont change at all?

---

<div class="post-metadata">

### Author: ![katharina\_hecker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/katharina_hecker/32/3431_2.png) [@katharina\_hecker](https://discourse.slicer.org/u/katharina_hecker)
#### Post date: [June 15, 2018, 6:13am UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/25 "2018-06-15T06:13:39Z")

</div>

Dear Mr. Lasso,  
I was running the following code:

load ‘matrixfilteredvolume.mat’

Matrix = FilteredVolume

cli\_imagewrite(inputParams.Matrix)

img.pixelData = Matrix;  
img.ijkToLpsTransform = [0.05 0 0 0; 0 0.05 0 0; 0 0 0.05 0; 0 0 0 1];

cli\_imagewrite(onputParams.Matrix, img)

and I´m getting the error:  
Not enough input arguments  
error in: cli\_imagewrite(inputParams.Matrix)

Which kind of inputParams I should change or which parameters are actually missing?

Thank you so much for your help!

---

<div class="post-metadata">

### Author: ![katharina\_hecker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/katharina_hecker/32/3431_2.png) [@katharina\_hecker](https://discourse.slicer.org/u/katharina_hecker)
#### Post date: [June 15, 2018, 8:26am UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/26 "2018-06-15T08:26:15Z")

</div>

Dear Mr. Lasso,  
I was thinking over my code again and came to the following conclusion, which I took from your poster:

[http://perk.cs.queensu.ca/sites/perkd7.cs.queensu.ca/files/Lasso2014b-poster.pdf](http://perk.cs.queensu.ca/sites/perkd7.cs.queensu.ca/files/Lasso2014b-poster.pdf)

I tried to adjust all the parameter and tried to run this code:

load(‘matrixfilteredvolume.mat’)

Volume = (FilteredVolume)

Matrixin=cli\_imagewrite(inputParams.Volume);

img.pixelData = Matrixin;  
img.ijkToLpsTransform = [0.05 0 0 0; 0 0.05 0 0; 0 0 0.05 0; 0 0 0 1];

outputvol = Matrixin

cli\_imagewrite(inputParams.outputvolume, outputvol);

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/1/91558df0d74f1d93e4c565aa54b490eb790b53bb.png)

I would be very pleased about any suggestions!

---

<div class="post-metadata">

### Author: ![katharina\_hecker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/katharina_hecker/32/3431_2.png) [@katharina\_hecker](https://discourse.slicer.org/u/katharina_hecker)
#### Post date: [June 15, 2018, 8:33am UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/27 "2018-06-15T08:33:06Z")

</div>

PS: Error Not enough input arguments in line:

Matrixin=cli\_imagewrite(inputParams.Volume);

---

<div class="post-metadata">

### Author: ![katharina\_hecker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/katharina_hecker/32/3431_2.png) [@katharina\_hecker](https://discourse.slicer.org/u/katharina_hecker)
#### Post date: [June 16, 2018, 8:30am UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/28 "2018-06-16T08:30:32Z")

</div>

after looking over the whole discussion, I’m coming to this conclusion:

matlab code:

load(‘matrixfilteredvolume.mat’);

img.ijkToLpsTransform = [0.05 0 0 0; 0 0.05 0 0; 0 0 0.05 0; 0 0 0 1];

img.pixelData=FilteredVolume

cli\_imagewrite(img.ijkToLpsTransform.FilteredVolume, img);

* * *

it seems to be the right path but I still got one error to solve:

img =

struct with fields:

```
ijkToLpsTransform: [4×4 double]
        pixelData: [181×299×305 double]

```

Struct contents reference from a non-struct array object.

Error in afterMoritz (line 19)  
cli\_imagewrite(img.ijkToLpsTransform.FilteredVolume, img);

I am thankful for all kind of suggestions!

---

<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: [June 16, 2018, 2:31pm UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/29 "2018-06-16T14:31:08Z")

</div>

There is no such thing as img.ijkToLpsTransform.FilteredVolume. Follow the working example more closely. Something like this should work:

cli\_imagewrite(inputParams.outputvolume, img);

---

<div class="post-metadata">

### Author: ![katharina\_hecker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/katharina_hecker/32/3431_2.png) [@katharina\_hecker](https://discourse.slicer.org/u/katharina_hecker)
#### Post date: [June 16, 2018, 4:06pm UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/30 "2018-06-16T16:06:05Z")

</div>

Thanks a lot Sir for your reply! I did now everything which you said:

load(‘matrixfilteredvolume.mat’); #loading of matrix

img.pixelData = FilteredVolume #using the codes you suggested

outputParams.min=min(min(min(img.pixelData))); #following the working example more closely  
outputParams.max=max(max(max(img.pixelData)));

img.ijkToLpsTransform = [0.05 0 0 0; 0 0.05 0 0; 0 0 0.05 0; 0 0 0 1];

cli\_imagewrite(inputParams.outputvolume, img); #use the cli\_imagewrite code to import the data like you said in the last post

Still I seem to miss something out, since the error appears:

Not enough input arguments.

Error in matrixvolumeto3dslicer (line 23)  
cli\_imagewrite(inputParams.outputvolume, img);

I really appreciate your effort and am very thankful for your suggestions.

---

<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: [June 16, 2018, 4:41pm UTC](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159/31 "2018-06-16T16:41:02Z")

</div>

Modifying an example script to use your data instead of similar sample data should be a site straightforward Matlab programming task. You should be able to find a solution by reflecting on the error message, experimenting, Google searching, or asking someone in your group to sit down with you and go through your code.

If you are just starting to learn Matlab, then I would recommend to consider learning Python instead, as most likely you’ll benefit from that more in the long term.

[Previous page](https://discourse.slicer.org/t/extensions-matlabbridge-send-transform-4x4-matrix-from-matlab-to-3d-slicer/3159.md?page=1)
