File Exchange

image thumbnail

Read Medical Data 3D

version 1.1 (54.5 KB) by

GUI to open most Medical file-formats: Dicom, V3D, ISI, NifTI, Analyze and other formats

32 Ratings



View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This function ReadData3D allows the user to open medical 3D files. It
supports the following formats :

Dicom Files ( .dcm , .dicom )
V3D Philips Scanner ( .v3d )
GIPL Guys Image Processing Lab ( .gipl )
HDR/IMG Analyze ( .hdr )
ISI Files ( .isi )
NifTi ( .nii )
RAW files ( .raw , .* )
VMP BrainVoyager ( .vmp )
XIF HDllab/ATL ultrasound ( .xif )
VTK Visualization Toolkit ( .vtk )
Insight Meta-Image ( .mha, .mhd )
Micro CT ( .vff )
PAR/REC Philips ( .par, .rec)





V : The 3D Volume
info : Struct with info about the data
Always the following fields are present
info.Filename : Name of file
info.Dimensions : Dimensions of Volume
info.PixelDimensions : Size of one pixel / voxel

The read functions are not fully implemented as defined in
the file-format standards. Do not use this function for
critical applications.

If you know an example file of GE_Version_4, GE_Version_5 or some other often used 3D medical format, please leave a comment (so I can implement that format).

Comments and Ratings (76)


B S (view profile)

i wanna open ''vtk'' format but it takes lot and dosent load. I can see this comment ''busy'' whats wrong whit it.
if would it possible plz mail me the answer or solution
thanks a million

Hi all
I am trying to read .mhd files accompanied with an .raw format. when I use this function for opening each of them I only can see some statistics. how can I see the output image? I need it for opening prostate MRI images.

Jihun Kwon

m b> It solved, thanks!

m b

m b (view profile)

If someone is having problems with mha_read_volume() that's because there is an error in the function. A SOLUTION to this problem is open up the mha_read_volume.m function and change this line fid=fopen(info.Filename','rb','ieee-le') to fid=fopen(info.Filename,'rb','ieee-le'). The single quote after Filename is causing an error.

Romilde Kotze

Hi. Thanks for the script. Have you perhaps managed to get the script working for a GE file? More specifically GE VolDICOM?

Zhiwei Zhai

There is one concern about the line 48 in function mha_read_volume. The datasize varied with the bitdepth in mha_read_volume, however, the datasize in function fread(fileID, datasize, 'type') is the number of elements. So, the datasize should be replaced by the number of elments, like follows:

% datasize=prod(info.Dimensions)*info.BitDepth/8;


Manon (view profile)

There is an error in the mha_read_volume.m file, line 26 and 28, fid=fopen(info.Filename','rb','ieee-le'), there's a quote in front of info.Filename', that prevents it from reading mha files, change both to: fid=fopen(info.Filename,'rb','ieee-le')

srinivas B

It is not working

nazli melek

when I open GUI,I cant load data and get this error: 'Struct contents reference from a non-struct array object.'
How can I use mfile to perform action?

Sai Zhang

How do I read a section of .dcm files?

Please help, thanks.

afsah saleem

I am facing problem in reading .mha files. my problem is i can read .mha file but don't know how to deal them as 2D image.

I am facing below problem when using 'mha_read_volume' .

Error using fopen.
First input must be a file name or a file identifier

I get the following when I try to run a .nii file:

Error using reshape
To RESHAPE the number of elements must not change.

Error in nii_read_volume (line 48)
V = reshape(V,info.Dimensions);

Error in ReadData3D>loaddata (line 379)

Error in ReadData3D>pushbutton_load_Callback (line 365)

Error in gui_mainfcn (line 95)

Error in ReadData3D (line 70)
gui_mainfcn(gui_State, varargin{:});

Error in>@(hObject,eventdata)ReadData3D('pushbutton_load_Callback',hObject,eventdata,guidata(hObject))

Error using uiwait (line 81)
Error while evaluating UIControl Callback


i used the codes but output is just one image wherein there are many raw images i uploaded in program. please help

i have cganged the datatype to 'single'.....then the following error shows:

Error using single
Not enough input arguments.

Error in mha_read_volume (line 57)
V = zlib_decompress(Z,single);

After running the mha_read_volume.m the following error is showing:

Undefined function or variable 'datatype'.

Error in mha_read_volume (line 57)
V = zlib_decompress(Z,datatype);

plz help


Chris (view profile)

Zhuotun Zhu

Hi All,

I have trouble when reading in the '.mhd' data while other said they loaded successfully. Here is the error:

Error using fopen
First input must be a file name or a file identifier.

Error in mha_read_volume (line 28)

Error in ReadData3D>loaddata (line 379)

Error in ReadData3D>pushbutton_load_Callback (line 365)

Error in gui_mainfcn (line 95)

Error in ReadData3D (line 70)
gui_mainfcn(gui_State, varargin{:});

Error in>@(hObject,eventdata)ReadData3D('pushbutton_load_Callback',hObject,eventdata,guidata(hObject))
Error using uiwait (line 81)
Error while evaluating UIControl Callback.

Anyone could point out why?
Thank you!

Usha Ram

I have BRATS image dataset. my query is, how to read, display and process those .mha files using this package. anybody please reply as soon as possible.


Tom (view profile)

There's a bug in mha_read_volume.m line 25 - the case statement is comparing a string ('true') against the first character ('t') so it always fails and will read everything as little endian. It should be:

% Open file
case ('t')

Tong Liang

Does anyone run into troubles when they try to load a NifTi image? Everytime I load a NifTi image, the volume is an empty array.

I'm trying to read images in raw format but I am not able to open.
I use the function ReadData3D but I do not get. could you help me? It´s very important!

no clear instructions for how to install or use this package


Alize (view profile)


960 (view profile)

When I read displacement field file in mha format by mha_read_volume.m
I have an error.That because the pixel in displacement is a vector.

I replace the

'V = reshape(V,info.Dimensions);' by
V = reshape(V,[str2num(info.ElementNumberOfChannels),info.Dimensions]);
V = reshape(V,info.Dimensions);
And it works.
Maybe it's useful for somebody. Thanks for your great job.I really enjoy using the mha reader.



When I run the above code following error occurs.Please help me to correct them.

Error in mha_read_header (line 10)

Output argument "info" (and maybe others) not assigned during call to "C:\Program

Error in proj (line 2)


I am having BRATS database of brain MR images with tumor in .mha format. How to read, display and process .mha files in MATLAB?

Ramon Casero

Thanks for the function. We could reuse the decompressor function in mha_read_volume() for our project. Credited here


I met with the following error while running the file mha_read_volume.What shall I do??

??? Input argument "info" is undefined.

Error in ==> mha_read_volume at 13
if(~isstruct(info)), info=mha_read_header(info); end

kehong zheng

I am trying to read a .raw file but I got this message:

>> ReadData3D
RescaleSlope not available, assuming 1
RescaleIntercept not available, assuming 0
could you help me ?

Samuel Smith

Really great package. Needed it to read .mha files for segmentation challenge. Worked perfectly.


rob (view profile)

Hi, I am trying to read .mhd files accompanied with an .zraw format. This somehow doesn't work? Do i need to turn the compression of?


sultan (view profile)

I made a simulation with SIMIND Monte Carlo Program for a jaszak phantom (with SPECT). I have some problems with the data formats and so i cannot manage to do reconstrcutin. Is there anyone experienced a problem like this? thanks..

Jim O'Doherty

Great set of tools - and very easily customized. Thanks for sharing


Arne (view profile)

okey - sorry I looked at the examples; it works fine with .mhd files. Great job!


Arne (view profile)

This tool would be super useful for me. I have 3D mhd-files I want to read into matlab. Ultimately, I want to print out the X,Y,Z-positions of the points.
But first I need to read the mhd's in.. yet Im totally new to MatLab and I don't know where to specify the files I want to read in.. Can someone help.. I guess I only need to copy paste the filename somewhere..


omar (view profile)



Stefan (view profile)

Had the same problem as Noha and aziesel. I am pretty sure it is because I was trying to read a 2D mha file. You probably need to convert it into a 3D file (:,:,1).


This program is ok. Works fine with the GUI. Totally fails if you have -nodisplay turned on. Makes it impossible for server/cluster use.


I'm getting the same error as Noha. Any input on this problem is appreciated!


Noha (view profile)

I am trying to read mha file and I get these errors
Error using reshape
To RESHAPE the number of elements must not change.

Error in mha_read_volume (line 78)
V = reshape(V,info.Dimensions);

Error in ReadData3D>loaddata (line 379)

Error in ReadData3D>ReadData3D_OpeningFcn (line 227)

Error in gui_mainfcn (line 221)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});

Error in ReadData3D (line 68)
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

any hints?


eman (view profile)

#Dirk-Jan Kroon
what about PGM 3d images?
how can i read them?


Adam (view profile)

Gah! I didn't want to make either of those ratings... those were accidents. I don't see a way to get rid of them. Oh well...


Adam (view profile)


Adam (view profile)


mat (view profile)

I am trying to read a .mha file but I got this message:

>> ReadData3D
RescaleSlope not available, assuming 1
RescaleIntercept not available, assuming 0


Mohamed (view profile)

it works fine for reading of Insight Meta-Image (.mha, .mhd)

Thank you


Jihun (view profile)

Chuck Hatt

I would suggest adding the following code to mha_read_volume.m, near line 77, if working with vector data:

if isfield(info,'ElementNumberOfChannels')
V = reshape(V,[info.Dimensions,str2double(info.ElementNumberOfChannels)]);
V = reshape(V,info.Dimensions);


Nic (view profile)

thank you! very helpful to check simulated data.


liu (view profile)

Nice tools.There's a GE Voldicom format, which can be exported from GE vivid ultrasound scanner.Can this format be read?

Max W.K. Law

I like this piece of tool. However, is it possible to take out the GUI? I have like 500 files need to be loaded and processed, and written the processed data back to the drive.

It takes about 30mins to finish one volume. I want everything to be automated so that my computer can work over weekend.


AP (view profile)

[SOLUTION]: To be able to read the par file correctly, add the following codes in par_read_header mfile:

RescaleIntercept = vald(12);
RescaleSlope = vald(13);
ScaleSlope = vald(14);

right after the line:

Then also add the following codes:

info.RescaleIntercept = RescaleIntercept;
info.RescaleSlope = RescaleSlope;
info.ScaleSlope = ScaleSlope;

after the line:



AP (view profile)

Another bug is that, par_read_header function does not really read 'RescaleSlope' and 'RescaleIntercept' and simply assumes 1 and 0, respectively. So it does not really matter whether we set real to true or not.


AP (view profile)

We may have the file extensions in either upper or lower case. Therefore, the following code:

if(strcmp(data.fileformat(i).ext(2:end),ext)), found=1; fileformatid=i; end

needs to be modified to:

if(strcmp(upper(data.fileformat(i).ext(2:end)),upper(ext))), found=1; fileformatid=i; end


AP (view profile)

Great tool! How can we bypass the dialog box and run everything from command line?


btw, how do I display the volumetric data on a .mha file once it has been loaded? I want to do volume rendering with it.


Really allows to avoid unpleaseant .dcm -> .nii trnasformations!

Dear friend,
Would you help me,
How to get geometry anatomy shape from DICOM using Matlab?

Maite Ruiz


I'm trying to read some .mhd and .mha files and I have some errors. I tried to change the code using your comments about this problem, but I still have some errors...
Could you maybe send me the code that worked well in your case?

Thank you in advance!


Paul Groot


First make sure to add the PAR/REC scripts to your path:


(or copy them to your current script folder)

Then just read the data in two stages:


The info struct is a collection of all the information in the PAR file. That's it!


atie (view profile)

hi, im trying to read .rec file. i thought i could use this tools because it stated there PAR/REC Philips ( .par, .rec) are among supported file. can anybody help?

slama najla

Hello, can some body send for me an example of 3d medical data to test it with an algorithm of watermarking because i didn't arrive to find it.thanks


Amedeo (view profile)

Good Tool.
Probably you should modify source code for saving the loaded volume.
Now you save only filename and fileformatid.

look at ReadData3D.m - line 398


Shihui (view profile)

There are some problems in reading my mhd file, which are list below.

Error using fseek
Invalid file identifier. Use fopen to generate a valid file

Error in mha_read_volume (line 36)

Error in ReadData3D>loaddata (line 379)

Error in ReadData3D>pushbutton_load_Callback (line 365)

Error in gui_mainfcn (line 96)

Error in ReadData3D (line 70)
gui_mainfcn(gui_State, varargin{:});

Error in

Error using waitfor
Error while evaluating uicontrol Callback


maya (view profile)

recently i have downloaded this tool.actually i have a setof 2D dicom slices and i converted to a 3D volume(.hdr) using a software,can i use your tool to read the intensity values of the voxels.I tried to use your tool,its giving error in the guifn
rror in ==> gui_mainfcn>local_openfig at 216
gui_hFigure = openfig(name, singleton, 'auto');

Error in ==> gui_mainfcn at 94
gui_hFigure = local_openfig(gui_State.gui_Name, gui_SingletonOpt);

Error in ==> ReadData3D at 68
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

pleases help

hi, I just have 2 or 3 corrections to your code if you are interested :

(1) : in par_read_volume.m - line 27 : replace 'short' by 'ushort'

(2) : in mha_read_volume - line 78 :

replace :

vector = info.Dimensions;

by :

if isfield(info, 'ElementNumberOfChannels')
vector = [str2double(info.ElementNumberOfChannels) info.Dimensions];
vector = info.Dimensions;

cheers :-)


Felx (view profile)

I had trouble reading data that I imported from DICOM (using SPM) and analyzed/modified through SPM. SPM saved the data in .hdr/.img files and I used the 'hdr_read_volume()' function to read it back into matlab. I fond that some datasets were read incorrectly and found that this happened when the value of the 'ImgDataType' in the .hdr file was 512. Both, the coustom read function ('get_info_analyze_hdr()') as well as Matlabs 'analyze75info.m' can't handle that value. After a little research I found that the datatype for 512 is DT_UNINT16 but this datatype is not known either so I tried DT_SIGNED_SHORT and the data is read in correctly.

So my fix was to add to strImgData in
'get_info_analyze_hdr()' the cell

strImgData{512}.ImgDataType = 'DT_SIGNED_SHORT';
strImgData{512}.ColorType = 'grayscale';

alternatively one could modify 'analyze76info.m' and add under

switch ImgDataType
case int16(512)
metadata.ImgDataType = 'DT_SIGNED_SHORT'

Siyi Deng

Siyi Deng (view profile)

great tool!


For the record, these are my changes:

***** old\mha_read_volume.m





***** old\mha_read_volume.m
V = reshape(V,info.Dimensions);

V = reshape(V,[info.NumberOfDimensions, info.Dimensions]);



Nice submission. I fixed two bugs in the parts which read .mha/mhd files (correct datasize --> work with oversized volume files; interpreting NumberOfDimensions --> work with 4D files). If interested, send me a private message with your email address and I will send you my updated files.

Sean de

Sean de (view profile)

Excellent! I've always wondered what was in those HDR files. We knew the dimensions of the IMG files ahead of time but this is a great tool for ones which we've lost and because the number of slices is not always uniform.




VTK bug fix by C. Aguilar

MATLAB Release
MATLAB 7.11 (R2010b)

Inspired: Medical Image Processing Toolbox

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video