MATLAB >> Bar Graph with Standard Error bars?

by Eric Brown » Tue, 04 Jan 2005 11:55:00 GMT

I am trying to generate a bar graph that also has standard error bars
and is grouped by the data from each row.

I have a matrix of mean values ('m') that is 5x3. The columns
corresond to times (0 min, 15 min, and 60 min in my case), and the
rows correspond to different anesthetics. There is also another 5x3
matix of the Standard Error values ('SE').

Using the 'bar' function I am able to plot the data from 'm' into 5
groups of 3 columns, but I can't label the groups with a character
string or add standard error bars. Can someone help me out here?
Maybe there's a different function I can use to make the bar graph.

Here's some sample data to work with:

% 5x3 Matrix of Mean Values
m =[ 100 200 110; 100 250 115; 100 300 150; 100 400 350; 100 325
200];

% 5x3 Matrix of Standard Error Values
SE = [10 10 7; 5 35 35; 9 14 23; 7 7 13; 4 11 24];

% This plots the data in a bar graph
bar(m)

MATLAB >> Bar Graph with Standard Error bars?

by Je » Tue, 04 Jan 2005 17:19:51 GMT


Hi,

try this

m =[ 100 200 110; ...
100 250 115; ...
100 300 150; ...
100 400 350; ...
100 325 200];

SE = [10 10 7; ...
5 35 35; ...
9 14 23; ...
7 7 13; ...
4 11 24];

b=bar(m);
set(gca,'Nextplot','add');

for i=1:length(b)

x(i,:)=mean(get(b(i),'XData'));
y(i,:)=max(get(b(i),'YData'));

end

x=reshape(x,1,size(x,1)*size(x,2));
y=reshape(y,1,size(y,1)*size(y,2));

SE=reshape(SE',1,size(SE,1)*size(SE,2));
tee=.03;

for i=1:length(x)

xerror=[x(i)-tee x(i)-tee x(i) ; ...
x(i)+tee x(i)+tee x(i)];
yerror=[y(i)+SE(i) y(i)-SE(i) y(i)+SE(i) ; ...
y(i)+SE(i) y(i)-SE(i) y(i)-SE(i)];

plot(xerror,yerror,'b-');

end

I customize the errorbar function.

help errorbar
edit errorbar

Maybe you have to modify the code and write your own function called
myerrorbar.

Je.

MATLAB >> Bar Graph with Standard Error bars?

by cudmore » Thu, 17 Feb 2005 04:45:52 GMT

Hey,

Thanks for the code. It works perfectly.

Bob

MATLAB >> Bar Graph with Standard Error bars?

by Judith » Sun, 05 Feb 2006 20:19:32 GMT

This is a great piece of coding that I found on this site for placing
error bars in a bargraph. However, it has a strange problem associated
with it.


m =[ 100 200 110; ...
100 250 115; ...
100 300 150; ...
100 400 350; ...
100 325 200];


SE = [10 10 7; ...
5 35 35; ...
9 14 23; ...
7 7 13; ...
4 11 24];


b=bar(m);
set(gca,'Nextplot','add');


for i=1:length(b)


x(i,:)=mean(get(b(i),'XData'));
y(i,:)=max(get(b(i),'YData'));


end


x=reshape(x,1,size(x,1)*size(x,2));
y=reshape(y,1,size(y,1)*size(y,2));


SE=reshape(SE',1,size(SE,1)*size(SE,2));
tee=.03;


for i=1:length(x)


xerror=[x(i)-tee x(i)-tee x(i) ; ...
x(i)+tee x(i)+tee x(i)];
yerror=[y(i)+SE(i) y(i)-SE(i) y(i)+SE(i) ; ...
y(i)+SE(i) y(i)-SE(i) y(i)-SE(i)];


plot(xerror,yerror,'b-');


end



Question:

If I run this program through once it works as it should be.
If I try a second time (or more) it doesn't work and comes up with the
following error message:


In an assignment A(matrix,:) = B, the number of columns in A and B
must be the same.

Error in ==> E:\Matlab_Files\Impact Damge\Comparison Graphs\bargraph.m
On line 22 ==> x(i,:)=mean(get(b(i),'XData'));

If I close down Matlab and then open Matlab again the program works
again, but subsequent attempts generate the above error (and so on)

I am using Matlab Version 6.0.0.88 Release 12.

Is this a bug ?

Is there anything that I can do about it?

Many thanks in advance

Judith

MATLAB >> Bar Graph with Standard Error bars?

by helper » Fri, 10 Feb 2006 09:13:23 GMT


This is happening because "x" is already defined in the workspace.
The same thing is happening here as happens when you run the code:

x = ones(3,3); % Define x as 3-by-3
x(:,2) = ones(4,1) % Try to assign 4-by-1 column

There are a few ways to avoid this.

1. Clear the workspace before running the code (using CLEAR on the
first line).
2. Turn this code into a function so it runs in its own workspace
(see "doc function").
3. Simply place the line of code:

x = []; y = [];

before the line:

for i=1:length(b)

MATLAB >> Bar Graph with Standard Error bars?

by Judith » Sun, 12 Feb 2006 14:53:27 GMT

Dear helper

Thanks. The program now works

Judith

Similar Threads

1. bars graph with standard deviation error

Hi,

I know how to create a bar graph. I use 'bar'.

Each bar on the graph is the average value of a series of numbers. Each series has, of-course, a standard deviation. 

I want to add to each bar two values: 
value + 1 std. dev.
value - 1 std. dev.

How can I do it with Matlab?

2. error bar on 2-D bar graph - MATLAB

3. Error Bar plot with std error bars

I need to plot an error bar plot where I have to show the
std error bars. 
Until now, I puted the Data for the bar plot into a 3x4
Matrix (I need 3 bar groups of 4 bars) and added 4 single
error plots with the std devation onto the same figure. Then
I had to move these std error plots on the x-axis so they
got on the right place over the corresponding bar what is a
huge effort. 
Has anyone an idea how this (little?) problem could be
solved easier? 
Google and the matlab help weren't my friends :(
Ou yeah, I'm working with Matlab 7.3.0 (R2006b).

4. range bar graph (floating vertical bar chart) - MATLAB

5. Changing the positioning of bars in 3d bar graphs

6. bar labels on a bar graph - MATLAB

7. how to fill bars in bar graph with texture

I was wondering how to fill the columns of a bar graph with textures (lines, dots, etc.) in addition to (or instead of) different colors (the default).  Is this possible in Matlab?

8. How to plot a bar graph with negative data and bars going upwards - MATLAB