Main Content

nrrdwrite

R2026b

Write volume to file using NRRD format

Since R2026b

    Description

    nrrdwrite(volume,filename) writes the medical volume volume to the NRRD file filename. The function derives valid NRRD0004 header fields from the available data.

    example

    nrrdwrite(volume,filename,metadata) specifies the additional metadata fields metadata.

    example

    nrrdwrite(___,Name=Value) specifies additional options, such as the encoding, endianness, spatial mapping, and patient coordinate system, using one or more optional name-value arguments.

    example

    Examples

    collapse all

    Read the image data from an NRRD format file. The file is part of a data set containing the 3-D CT and MRI scans from The Retrospective Image Registration Evaluation (RIRE) Dataset, converted to the NRRD file format. The original data set was provided by Dr. Michael Fitzpatrick. For more information, see the RIRE Project homepage. The size of the entire data set is approximately 35 MB. Download the data set from the MathWorks® website, then unzip the folder.

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

    Specify the name of the NRRD file to read.

    filename = fullfile(filepath,"supportfilesNRRD","Patient007CT.nrrd");

    Read the image data from filename. The image data V is a 3-D array of intensity values.

    V = nrrdread(filename);

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

    V = medfilt3(V);

    Write the filtered image to an NRRD file, using default header values.

    nrrdwrite(V,"Patient007CT_processed.nrrd")

    Read the image data from an NRRD format file. The file is part of a data set containing the 3-D CT and MRI scans from The Retrospective Image Registration Evaluation (RIRE) Dataset, converted to the NRRD file format. The original data set was provided by Dr. Michael Fitzpatrick. For more information, see the RIRE Project homepage. The size of the entire data set is approximately 35 MB. Download the data set from the MathWorks® website, then unzip the folder.

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

    Specify the name of the NRRD file to read.

    filename = fullfile(filepath,"supportfilesNRRD","Patient007CT.nrrd");

    Read the image data from filename as a medicalVolume object.

    medVol = medicalVolume(filename);

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

    V = medVol.Voxels;
    V = medfilt3(V);
    medVol.Voxels = V;

    The metadata of the volume remains the same after filtering. Use the metadata of the input file to write the output file, instead of using default header values. Extract the raw attributes of the image metadata into a structure.

    info = nrrdinfo(filename);
    addMetadata = info.RawAttributes
    addMetadata = struct with fields:
              dimension: '3'
                  sizes: '512 512 28 '
                   type: 'float'
               encoding: 'raw'
                 endian: 'little'
         spacedimension: '3'
            spaceorigin: '(1.653595, 1.653595, 5.000000)'
        spacedirections: '(0.653595,0.000000,0.000000) (0.000000,0.653595,0.000000) (0.000000,0.000000,4.000000)'
    
    

    Write the filtered image to an NHDR file with the raw attributes of the input file as the metadata. Specify GZIP encoding to reduce the size of the raw image data file.

    nrrdwrite(V,"Patient007CT_processed.nhdr",addMetadata,Encoding="gzip")

    Input Arguments

    collapse all

    Volumetric data, specified as a 3-D numeric array, medicalVolume object, or medicalImage object. If volume is a medicalVolume or medicalImage object, the function derives fields such as space, units, spaceorigin, spacedirections, and modality from the volume geometry mapping, if available, and writes them to the header.

    Name of the NRRD file with its extension, specified as a string scalar or character vector. If you specify a filename with the .nrrd extension, the function writes the volumetric data and header to a single file with the .nrrd extension. If you specify a filename with the .nhdr extension, the function writes the volumetric data to a raw image data file and the header to a separate file with the .nhdr extension.

    Data Types: char | string

    Additional metadata, specified as a structure. You can specify additional fields such as datafile, space, spaceorigin, spacedirections, kinds, units, and labels.

    Data Types: struct

    Name-Value Arguments

    collapse all

    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.

    Example: nrrdwrite(volume,"medData.nrrd",Endian="little") specifies that the endianness of the data in the NRRD file is little endian.

    Encoding format, specified as "raw", "gzip", or "ascii". Specify "raw" for speed and broad compatibility, "gzip" to reduce size, and "ascii" only when a text payload is explicitly required, as ASCII files are larger, and thus slower to parse.

    Data Types: char | string

    Endianness of the data, specified as "little" or "big". The function derives the default value from your machine format. Specify endianness based on the requirements of the downstream tool, if known. Endianness matters only for binary encodings.

    Data Types: char | string

    Spatial mapping information, specified as an affinetform3d object. Specify an affinetform3d object that reflects voxel sizes and orientation when the input is not a medicalVolume object. The function derives metadata such as space origin and space directions from the spatial mapping information.

    Patient coordinate system, specified as one of these values. Specify a coordinate system that matches how downstream tools interpret axes, if known.

    Patient Coordinate SystemDescription
    "right-anterior-superior" or "RAS"Anatomical coordinate system where positive X-axis is the right of the patient, positive Y-axis is the anterior of the patient, and positive Z-axis is the superior of the patient.
    "right-anterior-superior-time" or "RAST"3-D spatial coordinates correspond to the right-anterior-superior (RAS) coordinate system, but the fourth axis is time.
    "left-anterior-superior" or "LAS"Anatomical coordinate system where positive X-axis is the left of the patient, positive Y-axis is the anterior of the patient, and positive Z-axis is the superior of the patient.
    "left-anterior-superior-time" or "LAST"3-D spatial coordinates correspond to the left-anterior-superior (LAS) coordinate system, but the fourth axis is time.
    "left-posterior-superior" or "LPS"Anatomical coordinate system where positive X-axis is the left of the patient, positive Y-axis is the posterior of the patient, and positive Z-axis is the superior of the patient.
    "left-posterior-superior-time" or "LPST"3-D spatial coordinates correspond to the left-posterior-superior (LPS) coordinate system, but the fourth axis is time.
    "scanner-xyz"The 3-D spatial coordinate system of the scanner, not specified in terms of patient anatomy.
    "scanner-xyz-time"The 3-D coordinate system of the scanner, but with a fourth axis of time.
    "3D-right-handed"Generic right-handed 3-D spatial coordinate system, not specified in terms of patient anatomy.
    "3D-right-handed-time"3-D coordinate system of the generic right-handed coordinate system, but with a fourth axis of time.
    "3D-left-handed"Generic left-handed 3-D spatial coordinate system, not specified in terms of patient anatomy.
    "3D-left-handed-time"3-D coordinate system of the generic left-handed coordinate system, but with a fourth axis of time.
    "unknown"Patient coordinate system is unknown.

    Data Types: char | string

    Version History

    Introduced in R2026b

    See Also

    Objects

    Functions