File Exchange

image thumbnail

terrorbar.m: Error bars with controlled widths post-R2014b

version (10.4 KB) by Trevor Agus
Draws error bars (as line objects), allowing you to specify the width of the horizontal bars

1 Download

Updated 02 Aug 2015

View License

It is a known problem that the built-in "errorbar.m" sets the width of the error bars horizontal lines automatically (see, meaning that different graphs or lines might have inconsistently formatted error bars. Up to R2014a, there was a hack to get around this, but it doesn't work post R2014b.
This program draws error bars as specified, simply using the "plot" function to draw lines of appropriate size. You can specify their width relative to the x-axis size ("units") or in absolute terms ("centi", "inches", etc.). You can use it much like errorbar, but with extra terms, e.g.
>> h=terrorbar([1 2 3],[2 4 6],[.5 .7 .9],[.5 .7 .9],0.25,'centi');
>> h=terrorbar(xvalues,yvalues,lowererrorrbarheight,uppererrorbarheight,errorbarwidth,widthunits);
...and sensible variants of this also work (e.g. omitting the uppererrorbarheight gets you symmetric error bars).

NB This function doesn't draw a line through the points, nor bars for a bar chart. Use your usual other program to do that. Using "hold on" you can generally draw on the error bars before or after, depending on whether you want the error bars to be underneath the markers (normal for line graphs) or on to of them (normal for bar charts).

If needed you can tweak the error bar widths after, using this same function:
>> terrorbar(h,0.5,'centi')
>> terrorbar(h,0.5) %assumes same units as before
>> terrorbar(0.5,'centi') %is applied to all error bars in the current figure.

If you resize the figure after, the errorbars will cunningly update their width appropriately (e.g. if you specified their width in centimetres, they will remain as wide). However, if you update the axis width, the error bars may not behave as you want. You can always reinforce the original size requests by running terrorbar, even without parameters:
>> terrorbar

Known problems:

* Not yet programmed for use with a logarithmic x-axis

* May get stuck in a recursive loop if the error bars are very wide compared to everything else. But then that's a rather unlikely scenario...

* Not yet thoroughly tested, except for what I wanted it to do -- let me know if you find another bug.

Cite As

Trevor Agus (2021). terrorbar.m: Error bars with controlled widths post-R2014b (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

Zhaoqiang Liu

I also hope to have the logarithmic x-axis function soon.

Hong zhao

Could you provide the code for a logarithmic x-axis? Thanks

laurie stephey

gives me the flexibility I was looking for. thank you!

Jonas Stein

'DisplayName','mylegendtext', 'tag','data' is not necessary, because terrorbar creates only the errorbars, not the points.

Jonas Stein

terrorbar does not accept all parameters of the matlab errorbar() function. How can I convert the following function to terrorbar syntax?
errorbar(x, y, yErr, 'DisplayName','mylegendtext', 'tag','data');

Could you upload the source to a public githoster? Users could update and download and contribute easier then.


easy to use and clear

T. Leo Liu

Nice workaround for adjustable length of errorbars. Hope to have the logarithmic x-axis function soon. Thanks.

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

Inspired: superbar

Community Treasure Hunt

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

Start Hunting!