File Exchange

image thumbnail


version (6.29 KB) by Kenneth Morton
Create a bar plot with error bars.


Updated 01 Apr 2016

View Version History

View License

Barerrorbar() creates a bar plot with error bars. Although, not the first function to accomplish this task, barerrorbar() differs in that it directly calls the MATLAB functions bar() and errorbar() so that the options of both functions are preserved. For example:
x = 0.2*randn(3,4,100) + 1;
xMeans = mean(x,3);
xMeansL = repmat(3*0.2/10,3,4);
xMeansU = repmat(4*0.2/10,3,4);
barerrorbar({3:5,xMeans,'m'}, {repmat((3:5)',1,4),...
xMeans, xMeansL,xMeansU,'bx'});

Cite As

Kenneth Morton (2021). barerrorbar(varargin) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (12)

Andrea Gerbotto

Great! Thanks!


Xiaohui Liu

works like a charm for M > 1, but breaks down when M = 1. See below:

hold off;
M = 3;
N = 6;
x = rand(M, N);
lo = rand(M, N) * 0.5;
hi = rand(M, N) * 0.5;
% bar(1:M, x);
% hold on;
% errorbar(repmat((1:M)', 1, N), x, lo, hi, 'rx');
barerrorbar({1:M, x}, {repmat((1:M)', 1, N), x, lo, hi, 'rx'});


Since it is based on bar and error it is intuitive and easy to use. Got the plot I wanted in less than three seconds.


Try setting the groups by using handle as described in the bar function
>> set(h(1),'facecolor','white')
>> set(h(2),'facecolor','black')

Omar Mian

For the final figure in the example code, I want to set the first group of bars (at x = 1) black and the second group (at x = 2) red.

Adapting the example code, I assumed I could do this:
barerrorbar({1,xMeans(1,:),'k'}, {[1 1 1 1],xMeans(1,:), xMeansL(1,:),xMeansU(1,:),'bx'});
hold on
barerrorbar({2,xMeans(2,:),'r'}, {[2 2 2 2],xMeans(2,:), xMeansL(2,:),xMeansU(2,:),'bx'});

However, this gives an error. Could someone please advise on what the code should be?


Very useful, is there anyway to make the error bars one sided? By that I mean to only have the top half of the error bar showing as opposed to the entire thing?



Great! It is better than the other bar and error bar related code


Perfect. Every other bar/errorbar function on the FEX should be deleted.

Carolina Wählby

I tried hard to find this functionality in Matlab, but almost decided to do my plotting in Excel to get arrorbars on my barplots... Thanks!

Leah Acker

Great program.

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: BarsWithError

Inspired: superbar

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!