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

 Respuesta aceptada

Adam Danz
Adam Danz el 12 de Dic. de 2018
Editada: Adam Danz el 12 de Dic. de 2018
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 comentarios

Ellen Walker
Ellen Walker el 12 de Dic. de 2018
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.
Adam Danz
Adam Danz el 12 de Dic. de 2018
What line is producing that error?
errorbar(M2,err2)
Adam Danz
Adam Danz el 12 de Dic. de 2018
Editada: Adam Danz el 12 de Dic. de 2018
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.
Ellen Walker
Ellen Walker el 12 de Dic. de 2018
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
Adam Danz
Adam Danz el 12 de Dic. de 2018
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).
Ellen Walker
Ellen Walker el 12 de Dic. de 2018
Thank you, this helped!
Steven Lord
Steven Lord el 12 de Dic. de 2018
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2018a

Etiquetas

Preguntada:

el 12 de Dic. de 2018

Comentada:

el 12 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by