How to convert .mat to .nii?

15 ビュー (過去 30 日間)
Carlotta Fabris
Carlotta Fabris 2019 年 5 月 6 日
回答済み: Anudeep Kumar 2025 年 5 月 15 日
Hi!
I have a set of .mat files and I would like to convet them to .nii (Nifti) to conduct some GLM analysis.
The problem I have with these files is that when I load them in MATLAB, they are 1x111 cell. When I open a cell, it is 1x1435 double.
I tried converting them using make_nii but it tells me the files are not in the correct format. Any suggestions about how to do this?
Thank you so much,
Carlotta.

回答 (1 件)

Anudeep Kumar
Anudeep Kumar 2025 年 5 月 15 日
Hi Carlotta,
I believe the issue lies in the input not being of the shape as expected by ‘make_nii’ function. The .nii files are expected to be 2D/3D/4D arrays. You can achieve this by reshaping your cell into a 2D or 3D or 4D data based on your use case
According to the information provided we can reshape your data into a 2D array of size 111x1435 using ‘cell2mat’ and save it as nii.
Below is a short code snippet to do the same:
% Load the .mat file
data = load('example_data.mat');
mycell = data.example_cells; % Use the variable name from the .mat file
% Convert to matrix [111 x 1435]
mat_data = cell2mat(mycell'); % Transpose to get [111 x 1435]
nii = make_nii(mat_data);
save_nii(nii, 'example_output.nii');
You can reshape your data using ‘reshape’ to convert data into 3D as below :
% For demonstration, let's reshape to [35, 41, 111] since 35*41=1435
mat_data_reshaped = reshape(mat_data, [111, 35,41]);
mat_data_reshaped = permute(mat_data_reshaped, [2, 3, 1]); % [X Y T]
Here are the documentations for ‘cell2mat’, ’reshape’, and ‘permute‘ for your better understanding of these functions and use them as per your requirement.
Hope this helps!

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by