Would like to add standard error bars to bar graph

I am having difficulty getting error bars to appear on my bar graph. For some reason, it graphs the means but not the standard error. I appreciate any constructive advice.
This is the graph I keep getting:
Here is the relevant code:
AandBsumAex=find(AandB==1 & sess==0 & frAbase>=0);
AandBsumA2ex=find(AandB==1 & sess==1 & frAbase>=0);
FrAex=frAbase(AandBsumAex);
FrBex=frBbase(AandBsumAex);
FrA2ex=frAbase(AandBsumA2ex);
FrB2ex=frBbase(AandBsumA2ex);
M=[(mean(FrAex)),(mean(FrBex));(mean(FrA2ex)),(mean(FrB2ex))];
semmean=std(FrAex)./sqrt(4);
semmean1=std(FrBex)./sqrt(4);
semmean2=std(FrA2ex)./sqrt(9);
semmean3=std(FrB2ex)./sqrt(9);
err=[semmean,semmean1;semmean2,semmean3];
AandBsumAin=find(AandB==1 & sess==0 & frAbase<0);
AandBsumA2in=find(AandB==1 & sess==1 & frAbase<0);
FrAin=frAbase(AandBsumAin);
FrA2in=frAbase(AandBsumA2in);
FrBin=frBbase(AandBsumAin);
FrB2in=frBbase(AandBsumA2in);
M2=[(mean(FrAin)),(mean(FrBin));(mean(FrA2in)),(mean(FrB2in))];
semmean4=std(FrAin)./sqrt(7);
semmean5=std(FrBin)./sqrt(7);
semmean6=std(FrA2in)./sqrt(12);
semmean7=std(FrB2in)./sqrt(12);
err2=[semmean4,semmean5;semmean6,semmean7];
MA=subplot(2,1,1);
bar(M);
hold on
errorbar=[M,err];
hold off
MB=subplot(2,1,2);
bar(M2);
hold on
errorbar2=[M2,err2];
hold off

 Accepted Answer

You aren't plotting the error bars.
Use the errorbar function
errorbar(M, err)
However, you'll need to specify the x values too since your bars aren't on x =1, x=2, etc...
Another tip: When calculating standard error, instead of hard coding the length of FrAex and similar variables (which is a bad idea), just use length(FrAex).
semmean=std(FrAex)./sqrt(length(FrAex));

8 Comments

Tried this, this is what I get:
MA=subplot(2,1,1);
bar(M);
hold on
errorbar(M,err);
hold off
MB=subplot(2,1,2);
bar(M2);
hold on
errorbar2(M2,err2);
hold off
Index in position 1 is invalid. Array indices must be positive integers or logical values.
What line is producing that error?
Ahhh, I see. "errorbar2" isn't a function. Also, I updated my answer. You'll need to specify the x values of the errorbars since your bars aren't centered on x=1, x=2, etc in the plot you shared.
Thank you both for your help, but I still don't understand what to do to make the error bars appear on the graph.
I am not sure what you mean by 'x' values
First, take 2-3 minutes to read through the documentation on errorbar(). This should always be step 1 when using a new function. Here's that link again.
The plot you shared doesn't look like it was produced by the code you shared so I'm not sure how the bars on your barchart are centered. If bars 1 to n are not centered on x=1 to x=n, then you'll need to provide the x-values in the errorbar function.
If you still have trouble after reading through the documentation, provide the code here and describe the results (or inlucde another screen shot).
Thank you, this helped!
This error message:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
suggests to me that you didn't clear the workspace after running your original code (which defined a variable named errorbar) and the updated code (which tries to call the errorbar function.) In that case, MATLAB will treat errorbar(M,err) as an attempt to index into the variable. Using clear to clear the variable errorbar before running the updated code should resolve the problem if I'm correct about the cause.

Sign in to comment.

More Answers (0)

Products

Release

R2018a

Tags

Community Treasure Hunt

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

Start Hunting!