View License

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

» Watch video

Highlights from
Plot a 3D array using patch

5.0 | 8 ratings Rate this file 36 Downloads (last 30 days) File Size: 197 KB File ID: #28497 Version: 1.11
image thumbnail

Plot a 3D array using patch


Adam A (view profile)


17 Aug 2010 (Updated )

Plotting a 3D array using a patch surface mesh

| Watch this File

File Information

Plot a 3D array using patch
Adam H. Aitkenhead
The Christie NHS Foundation Trust
17th August 2010
This function enables a 3D array to be displayed using a patch surface mesh. To plot a 3D logical array, the function is called using the following syntax:
>> hpat = PATCH_3Darray(gridINPUT,gridX,gridY,gridZ);
Alternatively, a 3D numeric array can be plotted such that the colour of each facet corresponds to the value of each voxel in the array. The plot is generated using the following command. (Note that voxels which are not to be displayed should contain a value of NaN.)
>> hpat = PATCH_3Darray(gridINPUT,gridX,gridY,gridZ,'col');
The colormap to be used for the display of a 3D numeric array can be defined as follows:
>> cmap = jet(16);
>> hpat = PATCH_3Darray(gridINPUT,gridX,gridY,gridZ,cmap,'col');
Also, the colorbar lower and upper limits can be defined by the user as follows:
>> clim = [1,10];
>> hpat = PATCH_3Darray(gridINPUT,gridX,gridY,gridZ,clim,'col');
gridINPUT - 3D array of size (P,Q,R) - If not using the flag 'col', then gridINPUT should be a logical array. If using the flag 'col', then gridINPUT should be a numeric array, and all voxels which are not to be displayed should contain a value of NaN.
gridX (optional) - A 1xP array - List of the X axis coordinates.
gridY (optional) - A 1xQ array - List of the Y axis coordinates.
gridZ (optional) - A 1xR array - List of the Z axis coordinates.
cmap (optional) - A Nx3 array - The colormap definition. When plotting using the 'col' flag, cmap must be an Nx3 array, eg jet(32). When plotting a logical array, cmap must be an RGB triplet, eg [0.5,0.5,0].
clim (optional) - A 2x1 array - The colormap upper and lower limits.
'col' (optional) - When this flag is present, the surface is plotted using colours which correspond to the value in each voxel. In the input array gridINPUT, voxels which are not to be displayed should have a value of NaN.
'barN' (optional) - Display a colorbar on North of plot.
'barE' (optional) - Display a colorbar on East of plot.
'barS' (optional) - Display a colorbar on South of plot.
'barW' (optional) - Display a colorbar on West of plot.

hpat (optional) - Handle to the patch object.
hcbar (optional) - Handle to the colorbar.
For two examples, run the following code:

>> load exampleA.mat
>> figure
>> hpat = PATCH_3Darray(gridINPUT,gridX,gridY,gridZ);

>> load exampleB.mat
>> figure
>> cmap = jet(16);
>> hpat = PATCH_3Darray(gridINPUT,gridX,gridY,gridZ,'col',cmap);


This file inspired 2d And 3d Brain Plots.

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (15)
02 Dec 2016 UrsulaPR


Great work, Adam, thank you very much. How could I obtain a smoothed representation, so the voxels do not seem cubes? I tried with shading(gca, 'flat') in Matlab R2014a, but no success.

All the best,


02 Dec 2016 UrsulaPR

24 Jun 2014 Peter-Paul van Maanen

Hi Adam,

Great work!

I have to comment out line 511 in your file for the specified coordinates (gridX, gridY, gridZ) to work for my data:

%axis equal tight;

I get a 2D figure otherwise. I can send you my data if you want.



21 Jun 2013 Hassan

Hassan (view profile)

Great work Adam.
However, I get this error whenever I run your code on my data:

??? Undefined function or method 'PATCH_3Darray' for input arguments of type 'double'.

Any idea of why I am getting this error and what should I do to fix it?

Comment only
14 Nov 2012 arthur

arthur (view profile)

I found solution to my problem. It is just great, thank you once more.

Comment only
09 Nov 2012 arthur

arthur (view profile)

Hello Adam, thank you so much for your work. I was just wondering how can i get a GRIDinput of reasonable size filled with Nans from my gridX,Y,Z vectors? i am trying meshgrid but the results is way to big and i am exceeding memory capacity. Do you please have any hint regarding my problem?
Thanks you,

11 Feb 2012 zawaiter

thanks adaM GREAT WORK

Comment only
02 Feb 2012 zawaiter

thank you for the great job.i wish u can give me a hand with this,what change i should apply to ur function so i can define a colorbar range. so the voxels colores will be mapped according to its value. and if its value is bigger than the highst rang it will be mapped to the highst rang and if it is smaller than the lower range it will be mapped to the lowest value.

Comment only
13 Nov 2011 Grigory

You may extend it by adding Alpha feature
- after line 431 add: set(cell2mat(hpat),'FaceAlpha',alpha);
- or do it per cell basis one line earlier

2. after line 477 add: set(hpat,'FaceAlpha',alpha);

Alfa information could be specified as scalar, map or per element

Comment only
12 Nov 2011 Grigory

13 Aug 2011 Felipe

Felipe (view profile)

Grate Adam, thanks, It’s efficient and just what I was seeking.

12 May 2011 Adam A

Adam A (view profile)

Hi Rory,
Glad you're finding it useful. Regarding your problem, are you remembering to use the flag 'col' when you call the function? For example:
hpat = PATCH_3Darray(gridINPUT,gridX,gridY,gridZ,'col');

Comment only
12 May 2011 Rory Staunton

this is just what i was looking for, and super easy to use too!

my m x n x p array is mostly filled with NaN, and some values are in the range of ~100-10000. When I plot it with your function, everything is blue, as if the non-NaN values were all zero. Any clue how I can fix this?


06 Apr 2011 Elmar

Elmar (view profile)

13 Mar 2011 Knut

Knut (view profile)

I havent had a chance to test this code, but it seems that you are trying to solve what I tried in the submission below. Judging from your image attachements, it seems that your solution is more complete than mine.

Comment only
18 Aug 2010 1.1

Improved documentation

18 Aug 2010 1.2

Added missing example.mat file

10 Mar 2011 1.3

Now enables a 3D numeric array to be plotted such that each facet corresponds to the value of each voxel in the array.

11 Mar 2011 1.4

Minor bugfix

11 Mar 2011 1.5

The user can now specify the colour map for the display of a 3D numeric array.

24 Mar 2011 1.7

Bugfix: Prevent a hole from appearing at the lowest values in a full colour plot.

01 Apr 2011 1.8

Some minor code optimisations

07 Apr 2011 1.9

Provide the handle to the colorbar as an optional output.

10 Feb 2012 1.11

The user can now define the lower and upper limits of the colorbar using the input parameter 'clim'. Also, the input flag 'sym' has been removed as this can be done using clim instead.

Contact us