View License

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

» Watch video

Highlights from
Read Medical Data 3D

4.5 | 23 ratings Rate this file 306 Downloads (last 30 days) File Size: 54.5 KB File ID: #29344 Version: 1.1
image thumbnail

Read Medical Data 3D


Dirk-Jan Kroon (view profile)


10 Nov 2010 (Updated )

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

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

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).


This file inspired Medical Image Processing Toolbox.

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (53)
08 Feb 2017 Tom

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')

Comment only
08 Sep 2016 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.

Comment only
07 Sep 2016 Rafaela Pinto

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!

Comment only
28 Aug 2016 Chukwuka Orefo

no clear instructions for how to install or use this package

26 Apr 2016 Alize

Alize (view profile)

07 Apr 2016 960

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.

Comment only
07 Aug 2015 ANGAJA K


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)

Comment only
05 Aug 2015 ANGAJA K

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

Comment only
04 Aug 2015 Ramón

Ramón (view profile)

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

03 Aug 2015 ANGAJA K

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

Comment only
30 Jul 2015 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 ?

Comment only
27 Jul 2015 Samuel Smith

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

17 Jun 2015 rob

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?

07 May 2015 sultan

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..

Comment only
13 Jan 2015 Jim O'Doherty

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

21 Nov 2014 Arne

Arne (view profile)

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

21 Nov 2014 Arne

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..

Comment only
27 Oct 2014 omar

omar (view profile)


16 Oct 2014 Stefan

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).

Comment only
13 Jun 2014 Jameson

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

10 Jun 2014 aziesel

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

Comment only
04 Jun 2014 Noha

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?

Comment only
23 May 2014 eman

eman (view profile)

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

Comment only
06 May 2014 Adam

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...

Comment only
06 May 2014 Adam

Adam (view profile)

06 May 2014 Adam

Adam (view profile)

17 Apr 2014 mat

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

Comment only
26 Mar 2014 Mohamed

Mohamed (view profile)

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

Thank you

17 Feb 2014 Jihun

Jihun (view profile)

05 Dec 2013 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);

01 Sep 2013 Nic

Nic (view profile)

thank you! very helpful to check simulated data.

26 Jul 2013 liu

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?

Comment only
29 Apr 2013 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.

06 Feb 2013 AP

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:


Comment only
05 Feb 2013 AP

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.

Comment only
05 Feb 2013 AP

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

Comment only
05 Feb 2013 AP

AP (view profile)

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

10 Jan 2013 Antonio

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.

Comment only
11 Oct 2012 Gabriele

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

03 Oct 2012 Tim Monitoring

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

Comment only
02 Jul 2012 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!


Comment only
20 Jun 2012 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!

Comment only
19 Jun 2012 atie

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?

Comment only
12 Apr 2012 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

Comment only
30 Mar 2012 Amedeo

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

06 Mar 2012 Shihui

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

Comment only
04 Feb 2012 maya

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

Comment only
04 Oct 2011 Nicolas Toussaint

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 :-)

15 Jul 2011 Felx

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'

Comment only
26 May 2011 Siyi Deng

Siyi Deng (view profile)

great tool!

16 May 2011 Yannick

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]);


Comment only
18 Mar 2011 Yannick

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.

11 Nov 2010 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.


23 Feb 2011 1.1

VTK bug fix by C. Aguilar

Contact us