MATLAB: Am I unable to specify a vector for the “barwidth” input to BAR

MATLAB

I am trying to plot bars of different widths using the BAR function in a loop, like this:
axes
hold on
for n = 1:10
bar(n,sin(n),n/10)
end
However, this ignores the specified barwidth and produces a plot where the bars all have the same width. I would like to be able to pass a vector of barwidths, like this:
bar(1:10,sin(1:10),[1:10]/10)
However, this currently results in an error:
??? Error using ==> xychk
Too many input arguments.
Error in ==> bar at 53
[msg,x,y] = xychk(args{1:nargs},'plot');

Best Answer

The ability to pass a vector for barwidth is not available for BAR. To workaround this issue, you can modify the patch object after it is created as shown in the example below:
axes
hold on
for n = 1:10
h(n) = bar(n,sin(n));
% get the patch handle from the barseries object
p = get(h(n),'Children');
% get the XData and YData from the patch
xd = get(p,'XData');
yd = get(p,'YData');
% use x and y values to set Vertices
set(p,'Vertices',[[NaN;mean(xd) + [-n;-n;n;n]./20; NaN],[0;yd;0]]);
end