How to convert 3D .nii to .mat vector?
    5 views (last 30 days)
  
       Show older comments
    
I want to convert 3D matrix data of .nii to single array .mat file? Can you anyone provide the code pls?
0 Comments
Answers (1)
  Anudeep Kumar
 on 11 Mar 2025
        Hey Wasna, 
The niftiread() function can help you do that very easily.  
The niftiread(your_file), function reads and returns the volumetric data of your file in the specified variable which you can modify according to your needs. 
Below is a code snippet for your requirement. 
% Specify the path to your NIfTI file 
niiFilePath = 'path_to_your_file.nii'; 
% Read the NIfTI file 
niiData = niftiread(niiFilePath); 
% Convert the 3D matrix to a single array 
% This step converts the shape of data to a single array 
singleArray = niiData(:); 
% Specify the path for the .mat file 
matFilePath = 'output_file.mat'; 
% Save the data to a .mat file 
save(matFilePath, 'singleArray'); 
Here is a link to the documentation for niftiread(): 
I hope that helped! 
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
