View License

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

» Watch video

Highlights from
vol3d v2

4.7 | 26 ratings Rate this file 129 Downloads (last 30 days) File Size: 3.4 KB File ID: #22940 Version: 1.2
image thumbnail

vol3d v2



10 Feb 2009 (Updated )

3-d volume (voxel) rendering

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

An update to Joe Conti's popular vol3d function, allowing voxel colors and alpha values to be defined explicitly. Functionality is increased, but the function is entirely backwards compatible.

In cases where voxels can be any RGB color, use:
   vol3d('CData', cdata);
where cdata is an MxNxPx3 array, with RGB color along the 4th dimension. In cases where color and alpha values are highly independent, specify an MxNxP alphamatte as follows:
   vol3d('CData', cdata, 'Alpha', alpha);

This function amends the original (as little as possible) with the full permission of Joe Conti. The original function comes with some additional tools for editing colormaps and alphamaps which are not included in this version. See:


This file inspired Tactics Toolbox, Pcolor3, and Volumetric 3.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (43)
16 Feb 2017 Guojin Feng

Great tool for volume rendering. Many thanks.

16 Nov 2016 Aniya

Aniya (view profile)

16 Nov 2016 Nina

Nina (view profile)

15 Nov 2016 Nina

Nina (view profile)

Excellent!But I don't want the blue colour when the values in the matrix are Zeros. How to change the zero values into transparency.

26 May 2016 Tianxiao

Tianxiao (view profile)

Very nice tool, exactly what I want, to see the sulcus and gyri. However, there are big differences after 2014a. Seems like the images getting pixelized. And in 2014a and before, sometimes it appears artificial lines on the rendering. Could u give some hint ?

18 May 2016 S├ębastien Tosi

I'm looking for a way to minimize GPU memory usage when rendering rather large 3D image stacks with vol3D. This is a neat script but for "2D texture" mode I'm not sure how Matlab sends new image slices added to a plot to GPU: Is it as plain 2D GPU textures, or some other format including extra overhead? Also what format is used, I guess it involves conversion from double to some fixed point format, right?

Comment only
30 Nov 2015 Abubakar Yamin

Hello Oliver...

nice job pal...can you please provide me some report or presentation to understand the code. actually i have a very short time and have to submit a report. can you please help me in that...shall be grateful to you :)

27 Nov 2015 Abubakar Yamin

15 Jun 2015 Ibraheem Al-Dhamari

nice tool but the performance is not good as other medical visualization tools :(.

13 Feb 2015 Bui Quang

Hi Oliver,
In my case, the locations were converted to Cartesian coordinate from Polar coordinate using this code:

phi = ([1:300]/299)*2*pi;
r = [1:2000];
h = [0:9];
[PHI,R,H] = meshgrid(phi,r,h);
x = R.*cos(PHI);
y = R.*sin(PHI);
z = H.*0.3;

How can I specify the location (x,y,z) of my cData?

05 Feb 2015 Sriharsha

When i use the command
vol3d('CData', zeros(100,100,100));
i get a plot with complete blue colour.
I don't want the blue colour when the values in the matrix are Zeros.
I want 'White' colour, when the values are exactly Zeros.
How to change the colour 'Blue' to 'White'?

Comment only
29 Sep 2014 Ander Biguri

Ander Biguri (view profile)

21 Aug 2014 Javier Montoya

Javier Montoya (view profile)

Hi Oliver, nice visualization tool! Was wondering which is the best way to add labels on the axes? By using xlabel/ylabel/zlabel, the labels aren't displayed, especially if one rotates the 3d volume.

Comment only
25 Jun 2014 Darren Price

Wow! It has just made an almost impossible job very easy. Thanks! I will certainly be inspecting this code to learn a thing or two :)

06 Jun 2014 Alexandros Iliopoulos

25 May 2014 Yu

Yu (view profile)

11 Mar 2014 Zoidberg

29 Jan 2014 Ben

Ben (view profile)

Great contribution!

29 Dec 2013 Xiaoyin

24 Oct 2013 Hg

Hg (view profile)

18 Oct 2013 Jun Hao

Oliver, this is really brilliant!

I have another question. Is it possible to include interpolation while carrying out the 3D rendering if there is a certain element spacing between each slice?

Comment only
28 Aug 2013 Emmanuel Farhi

An excellent contribution for 3D rendering. However, the initial vol3d is not available anymore.

18 Aug 2013 William Warriner

Absolutely wonderful code. Very useful for visualizing voxel representations of part models for analysis of casting properties.

I made a slight change to the code on my own machine that has made some models a bit prettier. Specifically, at lines 204, 222, and 239, (drawing individual surface patches for x,y,z slices), I added a second line that adds 1 to the relevant dimension to draw the back surface of each voxel as well. While this obviously doubles the quantity of objects that need to be rendered, it makes very thin voxel sections (1 voxel thickness) more aesthetically appealing.

Perhaps an option to draw back surface patches? Just a suggestion.

Thank you for this code, I wouldn't be moving nearly as fast on my own work without it.

03 Apr 2013 Oliver Woodford

Naomi - The help text clearly states:
vol3d(...,'XData',x) 1x2 x-axis bounds. Default: [0 size(data, 2)].
vol3d(...,'YData',y) 1x2 y-axis bounds. Default: [0 size(data, 1)].
vol3d(...,'ZData',z) 1x2 z-axis bounds. Default: [0 size(data, 3)].
I have nothing more to add.

Comment only
03 Apr 2013 Naomi

Naomi (view profile)

How can I specify the location (x,y,z) of my cData?

Comment only
28 Feb 2013 Raz Shimoni

Great 3D visualization tool.

15 Jan 2013 Irvin

Irvin (view profile)

Excellent tool, works well for 3D probability density functions. Thanks.

08 Apr 2012 edward kabanyas

Oliver, it is possible to use vol3d for 3D map ? I have 3D data (latitude x longitude x time). I want to produce a volumetric plot, how to use vol3d for this ?

Comment only
03 Feb 2012 Mohamed Nassar

Oliver, Thank you so much, it works fine with me

03 Feb 2012 Oliver Woodford

Mohamed: If you run the demo you should discover that mri.mat is included with MATLAB. The 3D data is displayed using textured-mapped 2D planes, as described in the help. Call
to see a 3D view of the data.

Comment only
03 Feb 2012 Mohamed Nassar

Hello oliver, I used your code to display a 3D volume raw data, using this command "H= vol3d('cdata',datafile); it just displayed the 2D images on top of each other, looks like only a one 2d image, the datafile format is (x,y,z), where x is the number of frames, y *z is the 2d image's size, also at the end of the module, you load a mat file "mri.mat" which is not included, could you please advise me with that? thank you so much, you reply is highly appreciated

Comment only
20 Dec 2011 Jean-Philippe

This script works great to display MRI-like 3d images.

In the case that I have a logical 3d array (ie: a segmentation mask of an anatomical structure from an MRI), how can I use vold3d to only show the resulting mask? I am able to display my MRI model, but when I just want to display the mask I get a blank plot.

I = a [MxNxP] matrix for my MRI [grayscale values 0-255]
J = a [MxNxP] matrix for my mask [logical 0 or 1]

vol3d('cdata',I, 'texture','3D'); %this works
vol3d('cdata',J, 'texture','3D'); %this is blank

Thank you for your help.

Comment only
05 Dec 2011 Wilson

Wilson (view profile)

The hgsave command works, many thanks

05 Dec 2011 Oliver Woodford

Wilson: saveas requires a figure handle. vol3d returns a structure, hence the first error. The second error is due to the figure being too large. Try:
hgsave(gcf, 'Image3D.fig', '-v7.3');

Comment only
05 Dec 2011 Wilson

Wilson (view profile)

Nice code, but in my case, I can only render a 3D image but can not save the image.

When I use command:
h = vol3d('CData',Y); saveas(h,'Image3D.fig');
Matlab displays it's a 'Invalid handle'

If I save the image directly from figure, it displays:

Warning: Variable 'hgS_070000' cannot be saved to a MAT-file
whose version is older than 7.3.
To save this variable, use the -v7.3 switch.
> In hgsave at 93
In general\private\saveasfig at 7
In saveas at 126
In filemenufcn>localSaveExportHelper at 203
In filemenufcn>localSaveExport at 316
In filemenufcn at 56

And only a figure frame is saved instead of the actual image.

any ideas?

Comment only
15 Aug 2011 Oliver Woodford

The rendering method does stress MATLAB's rendering pipeline, so large volumes can slow down your machine significantly. Good graphics hardware helps a bit. Smaller datasets do too.

Comment only
14 Aug 2011 harish raj

matlab collapses if i run vol3d code

Comment only
24 May 2011 Maxwell

18 Apr 2011 Oliver Woodford

Matthias: The latest version allows you to set axis limits.

Comment only
10 May 2010 Oliver Woodford

Matthias: Unfortunately specifying different axis values isn't currently possible using this function. It is straightforward to implement though. You just need to change lines 121, 124 and 127 to achieve the desired results.

Comment only
08 May 2010 Matthias Pospiech

Wonderful script to generate 3D Volume plots.

However I need to set the axis values other than just the sizes of the data matrix.

How can one specifiy the axis (in x,y and z) for this plot?

Comment only
31 Dec 2009 Greg Siegle

Marvelous! Gorgeous 3d renderings of MRIs. Esp. in bone colormap.

09 Jul 2009 Jan Van den Bulcke


16 Apr 2011 1.2

Avoid destroying other vol3d objects when rerendering. Added a demo.

Contact us