How can I plot a one-bar stacked bar chart in MATLAB?

(This solution requires MATLAB 2019b)

Quoting the documentation:

bar(y) creates a bar graph with one bar for each element in y. If y is an m-by-n matrix, then bar creates m groups of n bars.

bar(x,y) draws the bars at the locations specified by x.

Using the first syntax, each element of a vector will become it's own bar. Using the second syntax, x defines how to understand a vector. In your case, you want a single stacked group:

bar(1,[1 2 3 4 5], 'stacked')

For comparison, with Y=rand(1,5): example


Hacky solution:

bar([1 2 3 4 5;0 0 0 0 0], 'stacked')
set(gca,'xlim',[0.25 1.75])

Daniel's answer is the way to go, but it only works in recent Matlab versions, starting at R2019b.

Ander's hacky solution works by creating a second, invisible bar. This has side effects; for example axis auto will extend the axes.

The following is an even hackier approach that avoids those issues. It creates the two bars and then removes the second by altering the graphical objects' data:

values = [1 2 3 4 5]; 
h = bar([values(:).'; NaN(1, numel(values))], 'stacked');
XData = vertcat(h.XData);
XData = num2cell(XData(:,1));
[h.XData] = XData{:};
YData = vertcat(h.YData);
YData = num2cell(YData(:,1));
[h.YData] = YData{:};