Why do I get more error bars than I defined?
Show older comments
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.
Thank you in advance!
clear all
clc
load ('data_repod_ad.mat');
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');
Accepted Answer
More Answers (2)
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
% load ('data_repod_ad.mat');
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
find(error ~= 0)
Theresa Kandels
on 10 May 2022
0 votes
Categories
Find more on Errorbars in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
