MATLAB: How to display a value on bar

plottingtext;

How can I display the value at the top of each bar? assume that I have the following bar plot:

Best Answer

Try this (if you have R2014b or later):
y1 = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
hBar = bar(y1,1); % Return ‘bar’ Handle
for k1 = 1:size(y1,2)
ctr(k1,:) = bsxfun(@plus, hBar(1).XData, hBar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = hBar(k1).YData; % Individual Bar Heights
text(ctr(k1,:), ydt(k1,:), sprintfc('%.1f', ydt(k1,:)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',8, 'Color','b')
end
the sprintfc function is undocumented. It is essentially the same as sprintf, and augmented to produce a cell array of outputs. This makes it very efficient in situations such as this, where the result is a cell array.