File Exchange

image thumbnail

nrrdWriter(filename​, matrix, pixelspacing, origin, encoding)

version by mdcacio
This function writes imagery and metadata to .nrrd or .nhdr medical imaging format files.


Updated 03 Dec 2014

View Version History

View License

This is a complement of the function nrrdread, "NRRD Format File Reader by Jeff Mather". It does exactly the opposite: it's a NRRD files writer.
- With filename it's possible to set the path, the filename and the format (nrrd or nhdr+data). For example, a filename like '/home/mariodiaz/testfile.nhdr' will create the file at the folder /home/mariodiaz, its filename will be testfile, and it will create the header (.nhdr) appart from the data (.raw, gzip,... depending on the encoding).
- matrix is the data to put into the file. It will also set some fields at the header like dimension, space dimensions, size, type, etc.
- Pixel spacing is equivalent to the output file voxel width and should be defined as [psx, psy, psz]
- Origin should be set to [0,0,0] by default (otherwise coordinates will be shifted)
- Encoding sets data encoding. 'raw', 'ascii' or 'gzip' available.

Cite As

mdcacio (2021). nrrdWriter(filename, matrix, pixelspacing, origin, encoding) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)


Hi, I' getting the same error as Bahareh.
writing works without problems - but if I try to read the file with nrrdread - then this gunzip error is produced.
Strange: At the beginning everything was fine, nrrds readable and all ok. It turned out not to work after ~10 runs.

Hi, I used this code for getting nrrd file but it is giving me this error: Unexpected end of input stream when attempting to GUNZIP the file

Does anyone know the solution? my input is : nrrdWriter('C:\Users\bsalafia\Desktop\LungProjection\LungProjectionMesh\LungProjectionSample.nrrd',I44, [1.00390625,1.00390625,3.00000000], [165.64453125,44.48046875,-657.00000000], 'gzip');


Hi all,

Thanks for the comments and contributions.

I'm afraid I haven't been able to check the code since I uploaded it, and I didn't encountered the issues mentioned in the comments back then - maybe I just used the function in a bounded way. Happily, I see some members of the Mathworks community managed to solve the issues. As I'm not coding / using Matlab from then I may not be able to solve any question, but I hope you will keep posting for the good of the community.

Thanks guys for the feedback.
All the best,


For those that were maybe having issues with it not doing proper cleanup (getting an error that the file did not exist or permission denied) this is because the function "writeData" was trying to delete a file before it had closed it. To change this go to that function, under the case {'gzip'} replace the code after the comment:

% Finally, we put this info into our nrrd file (fidIn)
fidTmpRaw = fopen(tmpFile, 'rb');
tmp = fread(fidTmpRaw, inf, [datatype '=>' datatype]);
cleaner = onCleanup(@() fclose(fidTmpRaw));
clearn2 = onCleanup(@() delete(tmpFile)); %DELETE THE FILE AFTER THE FUNCTION IS DONE AND FILE CLOSED
ok = fwrite (fidIn, tmp, datatype);

delete (tmpBase);
% delete (tmpFile); %COMMENT THIS OUT, THIS WAS CAUSING THE ISSUES (cleanup happens after function death)


Quan Chen

I think I found out why many of you got "unexpected end of input stream when attempting to gunzip the file"
In writeData function, when we reading from the gzipped file:
fidTmpRaw = fopen(tmpFile, 'rb');
tmp = fread(fidTmpRaw, inf, [datatype '=>' datatype]);
ok = fwrite (fidIn, tmp, datatype);
We shouldn't use [datatype '=>' datatype], the zip file has nothing to do with the datatype of your original data. Instead change it to 'uint8=>uint8', also change that in the fwrite. The final code should be:
fidTmpRaw = fopen(tmpFile, 'rb');
tmp = fread(fidTmpRaw, inf, 'uint8=>uint8');

ok = fwrite (fidIn, tmp, 'uint8');


lufei wang


Hi all.

Sorry I didn't answer so far. I'm not keeping an eye on this anymore; I think I added gzip option just as a feature but I didn't use it really.

In any case, thanks all for the comments. Hope you can improve the code for your benefit, and share anythink helpful.


Rhenan Bartels

Hii mdcacio, thank you very much for the code! It is helping a lot.

I had an issue when saving the file using gzip option. Do you have an e-mail so I can better explain the situation?


Rhenan Bartels

Urs Bohm

Hi, nice function. When I use gzip encoding it throws a warning and doesn't delete the temp zipped file. Adding fclose(fidtmpRaw) instead of the having in in the cleanup function works for me.

Ibraheem Al-Dhamari

Hey, I rotated the image and resized it back to the original size with int16 data type then saved it ti a new file using your function. When I read again using nrrdread, I got this error:
Unexpected end of input stream when attempting to GUNZIP the file
any idea?

Ibraheem Al-Dhamari

It works now,
I used this for the previous example

Ibraheem Al-Dhamari

thanks for your contribution :)
I am trying to use it but I still don't understand the input parameters.
Why they are different from the data I get from nrrdread function e.g:
type: 'int'
dimension: '3'
space: 'left-posterior-superior'
sizes: '483 483 161'
spacedirections: '(0.125,0,0) (0,0.125,0) (0,0,0.375)'
kinds: 'domain domain domain'
endian: 'little'
encoding: 'gzip'
spaceorigin: '(-30.1875,-30.1875,-30)'

Have a nice day!

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

Inspired by: NRRD Format File Reader

Inspired: nrrdmeta2num(imgh)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!