### MATLAB >> Bar Graph with Standard Error bars?

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?

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);

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?

Hey,

Thanks for the code. It works perfectly.

Bob

### MATLAB >> Bar Graph with Standard Error bars?

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);

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?

Judith

### MATLAB >> Bar Graph with Standard Error bars?

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?

Dear helper

Thanks. The program now works

Judith

```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?
```

```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).
```

```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?
```