%% Add Labels to the Ends of Bars
% Define vals as a matrix containing two series of data. Display the data in a horizontal bar graph and specify an output argument. Since there are two series, barh returns a vector of two Bar objects.

x = [1 2 3];
vals = [2 3 6; 11 23 26];
b = barh(x,vals);

%
% Display the values as labels at the tips of the first series of bars. To do this, get the coordinates of the tips of the bars by getting the XEndPoints and YEndPoints properties of the first Bar object. Since horizontal bar graphs have rotated axes, you must switch the values of XEndPoints and YEndPoints before passing them to the text function. Add a padding value of 0.3 to YEndpoints so that the text does not touch the edges of the bars. Then call the text function to display the labels.

xtips1 = b(1).YEndPoints + 0.3;
ytips1 = b(1).XEndPoints;
labels1 = string(b(1).YData);
text(xtips1,ytips1,labels1,'VerticalAlignment','middle')

%
% Next, follow the same steps to display the labels at the tips of the second series of bars.

xtips2 = b(2).YEndPoints + 0.3;
ytips2 = b(2).XEndPoints;
labels2 = string(b(2).YData);
text(xtips2,ytips2,labels2,'VerticalAlignment','middle')

% Copyright 2019 The MathWorks, Inc.