File Exchange

image thumbnail

vol3d v2

version (3.4 KB) by Oliver Woodford
3-d volume (voxel) rendering


Updated 24 May 2018

View Version History

View License

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

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, which came with some additional tools for editing colormaps and alphamaps, is sadly no longer available.

The way this rendering approach works is to render the six sides of each voxel as a translucent plane.

Cite As

Oliver Woodford (2021). vol3d v2 (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (61)

Eugene Pashinov

Hello. Nice tool, but could you change code to draw slices according to thay thickness, not fixed step?

Mostafa Sabra

i have 3d image created from multiple 2d dicom image and 3d image has empty and want to make it solid so can upload it into comsol multiphysics for simulation
my project is breast cancer ablation using RF

YongJun Choi

If you are using this function for creating 3D movie, use MATLAB 2016b or lower, all "cla" before the volume update, and fill zeros to NaN. This turns over 100 times faster with very low GPU memory use.

Sara Salimi

Thanks for sharing the tool.
I have a 3D volume and each object different value. How to change the colour of each object? Thanks

Anand P

How do i view 3D RGB images stacked one over the other as a volume using this tool ?

Luan Kuchanovicz

I am a beginner and I need help using this. I have a 3d array with the slices of a volume. I want to build the 3d Volume to see.
I have JPEG images I change to an array and create a 3d array. How I do the image?
I have done this.
I1=imread('Gap (1).jpg');
I2=imread('Gap (2).jpg');
I3=imread('Gap (3).jpg');
how i do the 3d image of mat?

Ege Keskin

This right here, saved my life. Thank you very much my friend...

Yavor Kamer

i like the code, but the syntax is confusing
from the description it looks like data is an X,Y,Z format but then from the Xdata syntax it turns out the first dimension is Y instead of X


ang chenun

Please please please

ang chenun

Is it ray casting? Could you explain it? Or recommend a document or something? thank you very much

Matt J

A quick question. I would like to make it so that if I set voxels near the surface of the volume to be semi-transparent (low alpha) and voxels near the core/interior of the colume to be opaque, that the viewer will be able to see through the translucent surface and observe the opaque objects in the interior. Can that be done with the tool as it now? If not, is it a difficult modification?

Oliver Woodford

Matt J: Thanks! Unfortunately I don't have the tools, so I've updated the description to reflect that. If you know where to find them, please let me know.

Matt J

A really great submission, but now that the original vol3d version has been removed from the FEX, maybe you'd consider including the colormap/alphamap editing tools that you allude to in the version 2 documentation?

Matt J

Stelios Fanourakis

I get the errors "Index exceeds matrix dimensions." at lines 145,115 please help

Otuekong Bassey

But pls can you give me a little a beginner but I want to apply this to a chemical Engineering problem
C=f (x,y,z)
C = 2.4:2.4:28.8%gotten from the function
Pls how will the rendering thing be applied

Joshua Ludwig

Guojin Feng

Great tool for volume rendering. Many thanks.




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.


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 ?

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?

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

Abubakar Yamin

Ibraheem Al-Dhamari

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

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?


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

Ander Biguri

Javier Montoya

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.

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




Great contribution!



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?

Emmanuel Farhi

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

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.

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.


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

Raz Shimoni

Great 3D visualization tool.


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

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 ?

Mohamed Nassar

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

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.

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


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.


The hgsave command works, many thanks

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


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?

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.

harish raj

matlab collapses if i run vol3d code


Oliver Woodford

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

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.

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?

Greg Siegle

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

Jan Van den Bulcke


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

Inspired: Volumetric 3, VOXview, pcolor3, TACTICS Toolbox

Community Treasure Hunt

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

Start Hunting!