Main Content


Write affine medical volume data to NIfTI file



write(medVol,filename) writes the voxel data and spatial information of the affine medicalVolume object medVol to the file filename in the Neuroimaging Informatics Technology Initiative (NIfTI) file format. The write function creates a combined NIfTI file that contains both metadata and volumetric data. The object function populates the metadata using appropriate default values and volume properties, such as size and data type.

write(medVol,filename,info) sets metadata attributes by using the metadata structure info. If the specified metadata structure does not match the image contents and size, then write returns an error.


collapse all

Write data from a medical volume object, created using a chest CT volume saved as a directory of DICOM files. The CT volume is part of a data set containing three CT volumes. The size of the entire data set is approximately 81 MB. Download the data set from the MathWorks® website, then unzip the folder.

zipFile = matlab.internal.examples.downloadSupportFile("medical","");
filepath = fileparts(zipFile);

Specify the directory of DICOM files for the first CT volume in the data set.

dataFolder = fullfile(filepath,"MedicalVolumeDICOMData/LungCT01"); 

Create a medical volume object for the CT volume.

medVol = medicalVolume(dataFolder);

The Voxels property contains the intensity values of each voxel. The VolumeGeometry property contains a medicalref3d object defining the spatial referencing for the image volume.

V = medVol.Voxels;
R = medVol.VolumeGeometry;

Modify the voxel data by applying a 3-D Gaussian filter.

sigma = 2;
filterV = imgaussfilt3(V,sigma);

Create a new medicalVolume object that contains the smoothed voxel values. To maintain the same spatial referencing as the original volume, specify the original medicalref3d object R.

medVolSmooth = medicalVolume(filterV,R);

Write the smoothed image data to a new NIfTI file.

niftiFilename = "LungCT01_smoothed.nii";

Input Arguments

collapse all

Medical volume, specified as a medicalVolume object. medVol must be an affine image volume.

An image volume is affine if these conditions are met:

  • All slices are parallel to each other.

  • The spacing between slices in each dimension is uniform.

  • The upper-left voxels of all slices are collinear.

  • No two slices are coincident, meaning no two slices are located at the same position in space.

Name of the NIfTI file, specified as a string scalar or a character vector. The write function creates a combined NIfTI file that contains both metadata and volumetric data and has the file extension .nii.

NIfTI file metadata, specified as a structure in the format returned by the niftiinfo function.

Version History

Introduced in R2022b