Adding Error Bars Within Multiple Data Sets
25 views (last 30 days)
Show older comments
Hi! This is my entire code below. The bar graphs is plotting perfectly, but I'm having trouble adding error bars to the two data sets within the graph. I have the error bar code at the end, but I'm getting errors when trying to index into my bar graph to add the code. What am I doing wrong?
Pre_Sys_Slow = [116 121 119 115 130 122 120 120];
Post_Sys_Slow = [112 118 116 110 126 120 118 120];
Pre_Dia_Slow = [68 75 75 70 84 79 79 78];
Post_Dia_Slow = [64 70 72 64 78 77 70 74];
sem1 = std((Pre_Sys_Slow)/sqrt(length(Pre_Sys_Slow)));
sem2 = std((Post_Sys_Slow)/sqrt(length(Post_Sys_Slow)));
sem3 = std((Pre_Dia_Slow)/sqrt(length(Pre_Dia_Slow)));
sem4 = std((Post_Dia_Slow)/sqrt(length(Post_Dia_Slow)));
semslow=[sem1 sem2; sem3 sem4];
av1 = mean(Pre_Sys_Slow);
av2 = mean(Post_Sys_Slow);
av3 = mean(Pre_Dia_Slow);
av4 = mean(Post_Dia_Slow);
%FAST
Pre_Sys_Fast = [121 129 120 118 124 120 122 130];
Post_Sys_Fast = [126 143 121 122 126 124 124 132];
Pre_Dia_Fast = [81 88 70 70 65 82 88 80];
Post_Dia_Fast = [87 105 79 78 72 85 92 88];
sem5 = std((Pre_Sys_Fast)/sqrt(length(Pre_Sys_Fast)));
sem6 = std((Post_Sys_Fast)/sqrt(length(Post_Sys_Fast)));
sem7 = std((Pre_Dia_Fast)/sqrt(length(Pre_Dia_Fast)));
sem8 = std((Post_Dia_Fast)/sqrt(length(Post_Dia_Fast)));
semfast=[sem5 sem6,sem7 sem8];
av5 = mean(Pre_Sys_Fast);
av6 = mean(Post_Sys_Fast);
av7 = mean(Pre_Dia_Fast);
av8 = mean(Post_Dia_Fast);
figure(1);
hold on
y=[av1 av2;av3 av4];
y1=[av5 av6;av7 av8];
b1 = bar([y;y1],'grouped');
ylim([60,130])
name= {'Systolic';'';'Diastolic';'';'Systolic';'';'Diastolic'};
title('Fast and Slow Tempo Blood Pressure Pre and Post Song')
ylabel('Blood Pressure (mmHg)')
set(gca,'xticklabel', name, 'fontweight', 'bold');
legend('Pre-Song','Post-Song')
% ERROR BARS BELOW!!!
% Calculate the number of groups and number of bars in each group
[ngroups,nbars] = size(y);
[ngroups1,nbars1] = size(y1);
% Get the x coordinate of the bars
x = nan(nbars, ngroups);
for i = 1:nbars
x(i,:) = b1(:,1).XEndPoints;
end
er=errorbar(x',y,semslow, 'k','LineStyle','none','LineWidth',1);
% Get the x coordinate of the bars
x = nan(nbars1, ngroups1);
for i = 1:nbars1
x(i,:) = b1(:,2).XEndPoints;
end
er1=errorbar(x1',y1,semslow, 'k','LineStyle','none','LineWidth',1);
0 Comments
Answers (1)
Trevor
on 22 Nov 2022
XEndPoints is a 1 by 4 array, while x is a 2 by 2 array. When you try to place the elements of XEndPoints into x, the dimensions are incompatable. You can use the "reshape" function to create x instead of filling in the elements with a for loop.
x = reshape(b1(:,1).XEndPoints, [2,2]);
er = errorbar(x,y,semslow, 'k','LineStyle','none','LineWidth',1);
This doesn't put the error bars in the correct location, because b1(:,1) is every other bar, while y is the first four bars in the chart. You can fix this by using this code instead, which takes the first two elements of both instances of XEndPoints and combines them:
x = reshape([b1(:,1).XEndPoints(1:2) b1(:,2).XEndPoints(1:2)], [2, 2]);
er = errorbar(x,y,semslow, 'k','LineStyle','none','LineWidth',1);
x1 = reshape([b1(:,1).XEndPoints(3:4) b1(:,2).XEndPoints(3:4)], [2, 2]);
er1=errorbar(x1,y1,semfast, 'k','LineStyle','none','LineWidth',1);
Note that the code provided causes er1 to look at semfast, as I was assuming that was your intention. Also, semfast needs to be declared as a 2 by 2 matrix instead of a vector.
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!