Would like to add standard error bars to bar graph

19 次查看(过去 30 天)
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

采纳的回答

Adam Danz
Adam Danz 2018-12-12
编辑:Adam Danz 2018-12-12
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 个评论
Steven Lord
Steven Lord 2018-12-12
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Errorbars 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by