File Exchange

image thumbnail

Adjust error bar width

version 1.3 (1.86 KB) by

Adjust error bars width in an errorbar plot to a ratio of the x-axis or a length (x-axis units).

29 Downloads

Updated

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

function errorbar_tick(h,w,xtype)
ERRORBAR_TICK Adjust the width of errorbars

ERRORBAR_TICK(H) adjust the width of error bars with handle H. Error bars width is given as a ratio of X axis lenght (1/80).

ERRORBAR_TICK(H,W) adjust the width of error bars with handle H. The input W is given as a ratio of X axis lenght (1/W). The result is independant of the x-axis units. A ratio between 20 and 80 is usually fine.

ERRORBAR_TICK(H,W,'UNITS') adjust the width of error bars with handle H. The input W is given in the units of the curent x-axis.

Comments and Ratings (38)

Kai Chan

doesn''t work

Farzaneh

it does not work in matlab R2015a. any solutions?

Wardo

Wardo (view profile)

I have used this in the past, but it does not seem to work with MATLAB R2015a.

RowlandPG

Hi,

The same as Tamas and Charlie, I too cannot save the plot in any way with the altered lengths, even attempting to print it to pdf reverts the lengths to automatic.

Does anyone have a workaround for this?

very good for the submission.

Hi
I would like to know if it is possible to include error bars with specific values on each point? (not a percentage)
Thanks

Tamas

Tamas (view profile)

Hi Matt,

For a non-experienced user, this file is a blessing to use with log graphs, however, I'm struggling with the same problem as Charlie - the changed ticks reset when trying to save the file. Do you have any workaround for this by any chance?

Hi Matt

Thank you for your update Matt. Works well in 2014b as long as you don't want to save the figure... When you save it (tried png, eps, fig) the size of the ticks go back to original. Any idea?

Very simple and works really well, however it receives four stars because I did have issues with a script breaking because of this. The issue turned out to be that the script would try to execute the command before the figure was generated, resulting in an error. Adding pause(1) before the errorbar_tick command solved this.

Matt

Matt (view profile)

In order to get this file working with 2014b you can replace the original if/else statement from the "Plot error bars" portion with the following:

if strcmpi(flagtype,'errorbar') % ERRORBAR(...)

x = h.Bar.VertexData(1,:); % Retrieve bar xdata from errorbar
dp = length(x)/3; % 3 data points per error bar
m = 1; % Multiplier for addition/subtraction
for ii = 1:dp
m = -1*m; % Switch between subtraction and addition
x(dp+ii:dp:end) = x(ii)+m*w/2; % Change xdata with respect to the chosen ratio
end
h.Bar.VertexData(1,1:end) = x;

else

error('Please enter an ErrorBar object!');

end

The .VertexData is an undocumented property of the .Bar object (which is also undocumented), used to draw the actually 'error bars' of an ErrorBar object. The first row of VertexData is somewhat of an equivalent to the 'xdata' property currently used by this function.

It seems a great and useful function, but it is not working with 2014b version.

hh=get(h,'children');
x = get(hh(2),'xdata');

hh = [ ] and so the error. But I cannot solve it. =(
Can someone?

Thanx!

Adam

Adam (view profile)

Always loved this function, but seems to be broken by 2014b :-(

John Trimper

I've used/loved this function for years, but something's going wrong for me tonight. Also the first time I've used it with MatLab 2014b (64bit).

With x values of [1:4], errorbar_tick is moving my 4th point over to an x-value of .08, so the updated series of x values becomes [1 2 3 .08]. I'm totally boggled. Any ideas?

Cengiz Gunay

Perfect!

Works great. For a graph with many errorbar plots this works:
>> h = get(gca,'Children');
>> % h is an array of plot handles
>> errorbar_tick(h(n),200); % etc.

Dan

Dan (view profile)

Works great for my linear axis. Thanks for sharing.

LDELBENE

What about if the x axis is in log scale?

J

J (view profile)

This function is really helpful!

Note that if you simply find and replace "x" with "y" throughout the code, it then works on horizontal error bars generated by, for example, errorbar_x.

Josh

Emily

Emily (view profile)

I really like this function, but I wish it also worked on horizontal error bars (FEX function herrorbar). Otherwise, this is very handy.

Min Liu

Solved my long-standing problem. Thank you~

You might want to insert this at line # 39:

if ~isscalar(h)
arrayfun(@(h) errorbar_tick(h,w,xtype), h);
return;
end

It's great that this function operates on the handle output by the errorbar function, rather than duplicating what the errorbar function already does.

Frank Coumans

Tom Etzer

Lin Li

Lin Li (view profile)

Anton Daneyko

Tim DuBois

Tobias

Tobias (view profile)

Thanks for sharing your solution! It works fine as long as I one have one error bar series in a plot but I can't get it to work when I have more (as in> errorbar, hold on, errorbar). Do you have a fix for this?

Max

Max (view profile)

Works great! Thanks a lot!

alex m

alex m (view profile)

good idea, but I'm new to matlab and am unsure of the variables h and xtype, could someone post an example?

Dan Knudsen

Thanks very much! Easy to use and works great.

One quick tip, which is relatively obvious but might be nice to add to the help: If you'd like to get rid of the ticks entirely, it's as easy as calling: errorbar_tick(h,0,'units')

Arnaud Laurent

Arnaud Laurent (view profile)

Re: thanks for your input Sathish, you're right the script wasn't made for log scale. I'll update the file to take care of that.

cool job!! but the feature does not retain when I change linear scale to log scale(i.e. the width is wider in log scale and is uncontrollable)

Joey Doll

Moritz

Moritz (view profile)

Nice work! This has driven me crazy in the past, but not any more .

Updates

1.3

The function has been updated to enable the use of a length as an input.
A new screenshot was made.

1.2

Screenshot updated

1.1

Header updated with examples, screenshot provided

MATLAB Release
MATLAB 7.5 (R2007b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video