MATLAB: Can I assign individual labels to polygons created using the scratch command

bar plotlabel bar plotpolygon labels

Suppose that I manually plot several rectangles using the scratch command, as in a bar plot, except these bars have varying widths and lie varying distances apart from one another — as in the example code shown below.
Is there a way to display a string label on the plot for each of these bars individually in one of the following ways?
  1. Each label lies within the plotted area of the bar that it corresponds to.
  2. Each label lies along the x-axis just below the midpoint of the bar that it corresponds to.
% ----------------------------------------------------------------------

y = [1 2 3];
LH = [1 5 8];
RH = [2 8 10];
colord = {'c','r','k'};
for ii = 1 : length(y)
patch([LH(ii) LH(ii) RH(ii) RH(ii)], [0 y(ii) y(ii) 0], colord{ii});
end
% ----------------------------------------------------------------------

Best Answer

Use the text() function
text(x, y, yourString, 'Color', [.2, .5, .4], 'FontSize', 30);
Try this:
colord = {'c','r','k'};
for ii = 1 : length(y)
patch([LH(ii) LH(ii) RH(ii) RH(ii)], [0 y(ii) y(ii) 0], colord{ii});
yourString = sprintf('y = %d', y(ii));
text(LH(ii), y(ii)+ 0.3, yourString, 'Color', colord{ii}, 'FontSize', 30);
end
ylim([0 5]);
grid on;
Related Question