# Why do I get more error bars than I defined?

3 views (last 30 days)
Theresa Kandels on 7 May 2022
Answered: Theresa Kandels on 10 May 2022
Hello,
I want to plot the mean curve of two measurements with 7 errorbars i calculated with the standard deviation.
However every time I try to plot the errorbars, more than I defined appear.
Could someone please tell me, what I'm doing wrong?
I also attached the figure I get.
clear all
clc
num_vector = length(data.n(2).gray_value);
error=zeros(1,num_vector);
% Mittelwert berechnen
for ii=1:num_vector
MW(ii) = mean (data.n(1).gray_value(ii)+data.n(2).gray_value(ii))/2;
time(ii) = mean (data.n(1).time(ii)+data.n(2).time(ii))/2;
if ii==40
error(ii)=sqrt((data.n(1).gray_value(ii)-MW(ii))^2+(data.n(2).gray_value(ii)-MW(ii))^2)*2;
elseif ii==80
error(ii)=sqrt((data.n(1).gray_value(ii)-MW(ii))^2+(data.n(2).gray_value(ii)-MW(ii))^2)*2;
elseif ii==120
error(ii)=sqrt((data.n(1).gray_value(ii)-MW(ii))^2+(data.n(2).gray_value(ii)-MW(ii))^2)*2;
elseif ii==160
error(ii)=sqrt((data.n(1).gray_value(ii)-MW(ii))^2+(data.n(2).gray_value(ii)-MW(ii))^2)*2;
elseif ii==200
error(ii)=sqrt((data.n(1).gray_value(ii)-MW(ii))^2+(data.n(2).gray_value(ii)-MW(ii))^2)*2;
elseif ii==240
error(ii)=sqrt((data.n(1).gray_value(ii)-MW(ii))^2+(data.n(2).gray_value(ii)-MW(ii))^2)*2;
elseif ii==280
error(ii)=sqrt((data.n(1).gray_value(ii)-MW(ii))^2+(data.n(2).gray_value(ii)-MW(ii))^2)*2;
end
end
%% Plot erstellen
f = figure(3);
f.Position = [10 10 550 400];
k= [[0 0 0];[0 0 1];[1 0 0];[0 1 0];[0 1 1];[1 0 1]];
errorbar(time,MW,error,'Color',k(1,:),'LineWidth',1); hold on;
grid on
xlim([160 10000]);
xlabel('time in ms');
ylabel('mean gray value of ROI');

the cyclist on 7 May 2022
Edited: the cyclist on 7 May 2022
For most of your points, you are not plotting no error bar; you are plotting an error bar with zero error.
You could make the default error equal to NaN, and I think you will get what you want.
error_zero = [0 0 0.03 0.04];
error_nan = [nan nan 0.03 0.04];
y = [1 1.1 1.2 1.3];
figure
errorbar(y,error_zero)
figure
errorbar(y,error_nan)

Voss on 7 May 2022
Edited: Voss on 7 May 2022
error is a 1-by-284 vector (going by the .fig file) whose elements are all 0 except at indices 40, 80, 120, 160, 200, 240, and 280. Those are the only indices where error is calculated, by the code inside the for ii=1:num_vector loop, so when you use errorbar(...,error,...) you get 284 error bars, of which all but those 7 have magnitude zero.
Note that each of the lines of code under the if or an elseif inside that loop are identical, so the entire if/elseif/elseif/... construction could be implemented more simply as:
if ismember(ii,40:40:280)
error(ii)=sqrt((data.n(1).gray_value(ii)-MW(ii))^2+(data.n(2).gray_value(ii)-MW(ii))^2)*2;
end
but I don't think either of the if/elseif/elseif/... or if ismember(ii,40:40:280) is what you really intend to do, since those both ignore all elements of MW and time besides those 7.
clear all
clc
data = struct('n',struct( ... % random data struct array
'gray_value',{rand(1,284) rand(1,284)}, ...
'time',repmat({linspace(160,10000,284)},1,2)));
num_vector = length(data.n(2).gray_value);
error=zeros(1,num_vector);
% Mittelwert berechnen
for ii=1:num_vector
MW(ii) = mean (data.n(1).gray_value(ii)+data.n(2).gray_value(ii))/2;
time(ii) = mean (data.n(1).time(ii)+data.n(2).time(ii))/2;
if ismember(ii,40:40:280)
error(ii)=sqrt((data.n(1).gray_value(ii)-MW(ii))^2+(data.n(2).gray_value(ii)-MW(ii))^2)*2;
end
end
error
error = 1×284
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
find(error ~= 0)
ans = 1×7
40 80 120 160 200 240 280

Theresa Kandels on 10 May 2022

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by