# Load nii volumes from 3d Slicer to Matlab

**URL:** https://discourse.slicer.org/t/load-nii-volumes-from-3d-slicer-to-matlab/29685
**Category:** Support
**Tags:** transforms, matlab
**Created:** [May 27, 2023, 3:44am UTC](https://discourse.slicer.org/t/load-nii-volumes-from-3d-slicer-to-matlab/29685 "2023-05-27T03:44:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Anatole](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@Anatole](https://discourse.slicer.org/u/Anatole)
#### Post date: [May 27, 2023, 3:44am UTC](https://discourse.slicer.org/t/load-nii-volumes-from-3d-slicer-to-matlab/29685/1 "2023-05-27T03:44:57Z")

</div>

Operating system: Windows 10  
Slicer version: 3D Slicer 5.2.2.2  
Expected behavior: Get the same volumes with the same absolute position in world coordinates from 3d Slicer in Matlab  
Actual behavior: Get a slight position difference in world coordinates from 3d Slicer in Matlab

I loaded 2 nifti volumes on 3d Slicer and on Matlab. I tried to get the same world coordinates in Matlab as 3dSlicer.  
When visualising volume 1 and volume 2 in world coordinates with matlab I have a slight diffrence in positions.  
How to load in world coordinates nii volumes on Maltab ?

Code used below  
Code :  
% Referential where data are loaded  
Rref = imref3d([800 800 400],[-200 200],[-200 200],[-100 100]);

% Load volume 1 from 3d Slicer in Referential Rref  
Vol1=niftiread(fixedpath);  
Vol1=permute(Vol1,[2 1 3]);  
info=niftiinfo(niipath);  
tformload=affine3d(info.Transform.T);  
Vol1=imwarp(Vol1,imref3d(size(Vol1)),tformload,‘nearest’,‘FillValues’,min(Vol1(:)),‘OutputView’,Rref);

% Load volume 2 from 3d Slicer in Referential Rref  
Vol2=niftiread(movingpath);  
Vol2=permute(Vol2,[2 1 3]);  
info=niftiinfo(niipath);  
tformload=affine3d(info.Transform.T);  
Vol2=imwarp(Vol2,imref3d(size(Vol2)),tformload,‘nearest’,‘FillValues’,min(Vol2(:)),‘OutputView’,Rref);

Volumes on Slicer  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/2/020caeb127e58ae3f03ba53f588641096c7e71a8.jpeg)  
Volumes on Matlab

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/a/8a74faca744cbac098e7736027900c80c34ac775.png)

---

<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: [May 27, 2023, 3:58am UTC](https://discourse.slicer.org/t/load-nii-volumes-from-3d-slicer-to-matlab/29685/2 "2023-05-27T03:58:11Z")

</div>

Nifti is a highly problematic file format with lots of complexities and ambiguities.

I would recommend to save in NRRD file format instead and read/write in Matlab using nrrdread.m and nrrdwrite.m - see [here](https://github.com/PerkLab/SlicerMatlabBridge/tree/master/MatlabCommander/commandserver).
