MATLAB: Show Values on bar graph; above bar when positive values, below bar when positive values

bar graphshow negative values below barshow positive values above bar

Hi,
with the code below I built a bar graph and inserted the values on each bar.
xtips2 = b(1).XEndPoints;
ytips2 = b(1).YEndPoints;
labels2 = string(b(1).YData);
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
is there a way of inserting all negative values below the bar and positive values above?
Many thanks for your reply.

Best Answer

figure,
vals = randi([-10 10],1,10);
x = 1:10;
h = bar(x,vals);ylim([-12 12])
lbs1 = cellfun(@num2str,num2cell(vals),'UniformOutput',false);
cond = vals>0;
text(x(cond),vals(cond),lbs1(cond),'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
text(x(~cond),vals(~cond),lbs1(~cond),'HorizontalAlignment','center',...
'VerticalAlignment','top')