MATLAB: Colors in surf according to values

surface

When I ejecute:
Z = [5 10 10 5
5 10 10 5];
surf(Z)
I get a figure which has three faces, and It colors first face with blue and second and third with yellow. I would like to modify this painting with yellow in last face. My objective is to get figure has colors according to values in Z, that is,
  • 5 to 10 -> blue,
  • 10 to 10 -> yellow, and
  • 10 to 5 -> blue.

Best Answer

You can control surface color by setting color value, like the following. More details can be found in help page of surf function.
C = zeros([size(Z),3]);
C(1,1,:) = [0 0 1]; % blue

C(1,2,:) = [1 1 0]; % yellow
C(1,3,:) = [0 0 1]; % blue
surf(Z,C)