Main Content

niftiwrite

Write volume to file using NIfTI format

Description

example

niftiwrite(V,filename) writes the volumetric image data V to a file by using the Neuroimaging Informatics Technology Initiative (NIfTI) format. By default, niftiwrite creates a combined NIfTI file that contains both metadata and volumetric data. niftiwrite names the file filename, adding the .nii file extension. niftiwrite populates the metadata using appropriate default values and volume properties, such as size and data type.

niftiwrite supports both the NIfTI1 and NIfTI2 file formats. NIfTI1 is the default file format. To write NifTI data in the NIfTI2 format, use the syntax with Name,Value pair arguments. Specify the Version argument as 'NIfTI2'.

example

niftiwrite(V,filename,info) writes the volumetric data V to a file, including the file metadata from info. If the metadata does not match the image contents and size, then niftiwrite returns an error.

niftiwrite(V,filename,info,Name,Value) writes the volumetric data to a file, using options specified in Name,Value pairs.

Examples

collapse all

Load a NIfTI image by using its .nii file name.

V = niftiread('brain.nii');

Filter the image in 3-D by using a 3-by-3 median filter.

V = medfilt3(V);

Write the filtered image to a .nii file, using default header values.

niftiwrite(V,'outbrain.nii');

Read the metadata from a NIfTI file by using its .nii file name.

info = niftiinfo('brain.nii');

Read volumetric data from the file by using the file metadata.

V = niftiread(info);

Edit the Description metadata field of the file.

info.Description = 'Modified using MATLAB R2017b';

Write the volumetric data with the modified metadata to a new .nii file.

niftiwrite(V,'outbrain.nii',info);

Input Arguments

collapse all

Name of NIfTI file, specified as a string scalar or character vector. By default, niftiwrite creates a combined format file that contains both metadata and image data and has the file extension .nii. If you specify the 'Compressed' name-value pair, niftiwrite adds the file extension .nii.gz. If you set the 'Combined' name-value pair to false, then niftiwrite creates two files with the same name and different file extensions. One file contains the metadata associated with the volume and has the file extension .hdr. The other file contains image data and has the file extension .img.

Data Types: char | string

Volumetric data, specified as a numeric array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

File metadata, specified as a structure returned by the niftiinfo function.

Data Types: struct

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: niftiwrite(V,'outbrain.nii','Compressed',true)

Type of NIfTI file to create, specified as true or false. If true (the default), niftiwrite creates a single file with the file extension .nii. If false, niftiwrite creates a pair of files with the same name but with different file extensions: .hdr for the file containing metadata, and .img for the file containing the volumetric data.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Compress image data, specified as true or false. If 'Compressed' is true, then niftiwrite generates compressed files, using gzip, with the file name extension .gz.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Endianness of the data, specified as 'little', to indicate little-endian format (default) or 'big', to indicate big-endian format.

Data Types: char | string

NIfTI data format, specified as 'NIfTI1' or 'NIfTI2'.

  • If specified as 'NIfTI1', then niftiwrite writes the input according to NIfTI1 data format.

  • If specified as 'NIfTI2', then niftiwrite writes the input according to NIfTI2 data format.

  • If not specified, then the default value for 'Version' is chosen based on the maximum dimension of the input volumetric data.

    • If the maximum dimension of the input is less than or equal to 32767, then the default value is NIfTI1.

    • If the maximum dimension of the input is greater than 32767, then the default value is NIfTI2.

Data Types: char | string

References

[1] Cox, R. W., J. Ashburner, H. Breman, K. Fissell, C. Haselgrove, C. J. Holmes, J. L. Lancaster, D. E. Rex, S. M. Smith, J. B. Woodward, and S. C. Strother. "A (sort of) new image data format standard: NiFTI-1." 10th Annual Meeting of Organisation of Human Brain Mapping, Budapest, Hungary, June 2004.

Version History

Introduced in R2017b