Main Content

niftiread

R2026b

Read NIfTI image

Description

V = niftiread(filename) reads the NIfTI image file specified by filename in the current folder or on the path, and returns volumetric data in V. The niftiread function supports both the NIfTI1 and NIfTI2 file formats.

example

V = niftiread(headerfile,imgfile) reads a NIfTI header file (.hdr) and image file (.img) pair.

V = niftiread(info) reads a NIfTI file described by the metadata structure info. To create an info structure, use the niftiinfo function

example

Examples

collapse all

Load volumetric data from a NIfTI file. The file uses the NIfTI combined format—the image and metadata are in the same file. This type of NIfTI file has the .nii file extension.

V = niftiread('brain.nii');

View the variable in the workspace.

whos V
  Name        Size                  Bytes  Class    Attributes

  V         256x256x21            1376256  uint8              

Read the metadata from a NIfTI file.

info = niftiinfo('brain.nii');

Read the volumetric image using the metadata structure returned by niftiinfo.

V = niftiread(info);

View the variable in the workspace.

whos V
  Name        Size                  Bytes  Class    Attributes

  V         256x256x21            1376256  uint8              

Input Arguments

collapse all

Name of the NIfTI file, specified as a string scalar or character vector. The file can be in the NIfTI1 or NIfTI2 file format.

  • If you do not specify a file extension, then niftiread looks for a file with the .nii extension.

  • If niftiread cannot find a file with the .nii extension, then it looks for a compressed version of the file, with extension .nii.gz.

  • If niftiread cannot find a file with the .nii.gz extension, then it looks for a file with the .hdr, .hdr.gz, .img, or .img.gz file extension.

  • If niftiread cannot find a file that matches any of these options, then it returns an error.

You can import valid data from URLs and remote locations, such as cloud services, for easier collaboration and data management. Depending on the location of your file, filename can take one of these forms.

Location

Form

Current folder or folder on the MATLAB® path

Specify the name of the file in filename.

Example: "medicalData.nii"

File in a folder

If the file is not in the current folder or in a folder on the MATLAB path, then specify the full or relative path name.

Example: "C:\myFolder\medicalData.nii"

Example: "\imgDir\medicalData.nii"

Uniform resource locator (URL)

If you specify the file as an internet URL, then filename must contain the protocol type, such as http://.

Example: "http://my_hostname/my_path/medicalData.nii"

Remote location

If the file is stored at a remote location, then filename must contain the full path of the file specified as a URL of the form:

scheme_name://path_to_file/my_file.ext

Based on the remote location, scheme_name can be one of the values in this table.

Remote Locationscheme_name
Amazon S3™s3
Windows Azure® Blob Storagewasb, wasbs
HDFS™hdfs

For more information, see Work with Remote Data.

Example: "s3://my_bucket/my_path/medicalData.nii"

The function downloads each new remote file only once per session to a temporary directory. If you access the same remote data again during the same MATLAB session, the function uses the downloaded file from the temporary directory.

Data Types: char | string

Name of the file containing metadata, specified as a string scalar or a character vector. The NIfTI header file (.hdr) holds the metadata associated with a NIfTI volume. If you do not specify a corresponding imgfile, then niftiread looks in the same folder for a file with the same name and extension .img.

Data Types: char | string

Name of the file containing volume, specified as a string scalar or a character vector. The NIfTI image file (.img) holds the volume data. If you do not specify a corresponding header file, then niftiread looks in the same folder for a file with the same name and extension .hdr.

Data Types: char | string

NIfTI file metadata, specified as a structure returned by niftiinfo.

Data Types: struct

Output Arguments

collapse all

Volumetric data, returned as a numeric array.

More About

collapse all

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

expand all