I tested in version R2018b. The colours of the bars are determined by the FaceColor
and EdgeColor
properties by default, not directly by the CData
property. The CData
property is only used when FaceColor
is set to 'flat'
.
Here is a short example using bar(Y)
.

n_groups = 4;
n_data_per_group = 5;
Y = rand(n_groups, n_data_per_group);
h = bar(Y);
set(h, 'FaceColor', 'flat');
for i = 1:n_data_per_group
h(i).CData = Y(:, i);
end
colorbar
Alternatively, as suggested by gnovice's solution in the other thread, one can use bar(Y, 'hist')
to make Patch
objects instead of Bar
objects. A slight modification in his example to accommodate for multiple groups of data is given below.

n_groups = 4;
n_data_per_group = 5;
Y = rand(n_groups, n_data_per_group);
h = bar(Y, 'hist');
set(h, 'CDataMapping', 'scaled');
for i = 1:n_data_per_group
h(i).CData = Y(:, i);
end
colorbar
The main observable difference between the two methods is the default spacing between the bars.