How to import in nilearn 3D matrices representing brain data from Matlab?

I have worked with fMRI data in matlab as 3D and 4D matrices. I would like to export them to nifti and use them with Nilearn. I have used some export functions, but these so not contain any affine transformation information, so it seems this is a problem to visualize properly. How to fo about this problem?

Hi @AdelleBernal,

You can load your data into Python using scipy.io.loadmat — SciPy v1.10.1 Manual. Then, you can use nilearn.image.new_img_like to create a new image from the data, given a reference image and affine you want your data to correspond with. Then you can save it out with the .to_filename method.

Best,
Steven

1 Like

Thanks. How would I know the affine I want? Sorry if this is too of a newbie question!

Without knowing more about your workflow it is hard to tell. But if you didn’t do any resampling or warping, you can likely use the same affine as the fMRI data you originally worked with.

You can get that affine with

img = nilearn.image.load_img('path/to/img.nii.gz')
affine=img.affine
1 Like