MATLAB: How to change the color of each bar in a plot if I do not have bar groups in MATLAB 7.6 (R2008a)

MATLAB

I use the following code to create a bar plot:
x = [46.6 -34.5 47.9];
bar(x)
I would like each bar to display a different color.

Best Answer

The following is an example of changing color of each bar by changing the color of the patches that compose the bars:
x = [46.6 -34.5 47.9]';
bhandle = bar(x);
ch = get(bhandle,'Children'); %get children of the bar group
fvd = get(ch,'Faces'); %get faces data
fvcd = get(ch,'FaceVertexCData'); %get face vertex cdata
[zs, izs] = sortrows(x,1); %sort the rows ascending by first columns
for i = 1:length(x)
row = izs(i);
fvcd(fvd(row,:)) = i; %adjust the face vertex cdata to be that of the row
end
set(ch,'FaceVertexCData',fvcd) %set to new face vertex cdata