File Exchange

image thumbnail


version 1.0.1 (21.2 KB) by Addison Elliott
MATLAB library that provides easy-to-use functions for loading and saving NRRD files.


Updated 16 Aug 2018

GitHub view license on GitHub

matnrrd is a MATLAB library that provides easy-to-use functions for loading and saving NRRD files. One feature that sets matnrrd apart from other NRRD readers is that it parses the metadata fields into sensible datatypes.
To use this library, copy/paste nrrdread.m and nrrdwrite.m somewhere in your MATLAB path. You are ready to go!


[data, metadata] = nrrdread('data/test1d_ascii.nrrd');
[data, metadata] = nrrdread('data/test3d_bigendian_raw_noendianfield.nrrd', 'Endian', 'big');

[data, metadata] = nrrdread('data/test1d_ascii.nrrd');
metadata.encoding = 'raw';
nrrdwrite('test.nrrd', data, metadata);


One of the main advantages of this function is that the metadata is
parsed from strings and turned into a sensible datatype. This is
performed for the fields specified in the NRRD specification.

A structure is used to store the metadata fields in MATLAB. One caveat
of using a structure is that the keys cannot have spaces in them.
Since the NRRD format includes fields with spaces, the spaces are
removed when reading. A fieldMap key is added to the structure that
contains a Nx2 cell array. The first column signifies the key name in
the MATLAB metadata structure and the second column contains the actual
field name with spaces. This preserves the spaces in field names when
using nrrdwrite to save an NRRD file.

Here is a list of supported fields and their corresponding MATLAB
datatype they are converted to:
* dimension - int [REQUIRED]
* lineskip - int
* byteskip - int
* space dimension - int
* min - double
* max - double
* oldmin - double
* oldmax - double
* type - string [REQUIRED]
* endian - string
* encoding - string [REQUIRED]
* content - string
* sampleunits - string
* datafile - string
* space - string
* sizes - 1xN matrix of ints [REQUIRED]
* spacings - 1xN matrix of doubles
* thicknesses - 1xN matrix of doubles
* axismins - 1xN matrix of doubles
* axismaxs - 1xN matrix of doubles
* kinds - Nx1 cell array of strings
* labels - Nx1 cell array of strings
* units - Nx1 cell array of strings
* spaceunits - Nx1 cell array of strings
* centerings - Nx1 cell array of strings
* spacedirections - MxN matrix of doubles
* spaceorigin - MxN matrix of doubles
* measurementframe - MxN matrix of ints

Most of the fields listed in the table above are optional with the
exception of four. The NRRD file must contain the type, dimension,
sizes and encoding fields.

Note: For spacedirections, NRRD allows specifying none for a
particular dimension to indicate it is not a spatial domain.
NRRDREAD will make the first row of the matrix all NaN's to signal
that it is none for the dimension. For example:
space directions: none (1,0,0) (0,1,0) (0,0,1)
will turn into:
[NaN NaN NaN; 1 0 0; 0 1 0; 0 0 1]

For unsupported fields, a warning will be displayed and the value will
be left as a string.

Help out by reporting bugs or contributing code at:

See the format specification online:

Comments and Ratings (2)



Add support for custom fields in nrrdread and nrrdwrite

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: NRRD Format File Reader

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor