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

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

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

## Relevant Pages

• Bar Graph with Standard Error bars?
... error bars in a bargraph. ... it has a strange problem associated ... If I close down Matlab and then open Matlab again the program works ...
(comp.soft-sys.matlab)
• Re: updating states in SIMULINK / interaction with MATLAB workspace
... I am using certain variables that I am controlling my model towards ... indexing and referencing matlab arrays inside simulink. ... Make Matlab functions inside Simulink that would update my ... workspace based on certain logical inputs from simulink. ...
(comp.soft-sys.matlab)
• Re: read base workspace from function workspace
... valid MATLAB expression, in the context of the workspace ws. ... or the workspace of the caller function. ... evalin output argument list is recommended over including the output ...
(comp.soft-sys.matlab)
• Re: read base workspace from function workspace
... Did you look at the evalin() function? ... valid MATLAB expression, in the context of the workspace ws. ... or the workspace of the caller function. ...
(comp.soft-sys.matlab)
• Re: Matlab Compiler - General Program Design Question
... created by the pre-computational code. ... This creates and leaves behind the data in the workspace. ... Use persistent variables for the output of p1, you could use a 'flag' persistant variable to ensure p1 gets called only once. ... I found this interesting way to communicate between two instances of MATLAB using a memory mapped file. ...
(comp.soft-sys.matlab)