MATLAB: Trying to add color gradient to patch(isosurface))

colorgradientcolormapimagingisosurfaceMATLABpatch

Hi all,
im currently trying to visualize a pressure field in a 3D voxel field of a 32x32x32 image.
The way this currently works is through using patch(isosurface()) as is shown in the code below.
image = rand(32,32,32);
filtered = imgaussfilt3(image,2.5);
BW = imbinarize(filtered,0.517);
patch(isosurface(BW),'FaceColor','white','EdgeColor','black')
The image above is what it produces.
What it does is plot the contour of the 1's and leave empty the 0's but I want it to do something else. The 1 values are going to be replaced by pressure field values (1 to 33) and I want the white+black contour to change into a color gradient. So far I couldnt get any colormap to do this. Is this even possible?

Best Answer

Here is an example (not tested)
% BW - is your 01 3d matrix
% A - is your color value matrix (0-33)
cm = jet(33); % colormap
fv = isosurface(BW,0.9);
fv.facevertexcdata = cm(A(:),:); % get color according to 0-33
patch(fv,'facecolor','interp')