Re: Bar Graph with Standard Error bars?



Judith wrote:


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



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