How to plot error bars for multiple data points at a single time point

11 views (last 30 days)
I am new on Matlab and trying to plot error bars on data points. But I have multiple data points at a single time point. Below down is my code:
EXP.t=[0 0 0 0 2.16 3.96 5.83 8.66 13.3 24]; %hours
EXP.Blood_15=[0.05 0.036 0.027 0.021 0.02 0.017 0.015 0.013 0.011 0.008]; %concentration
EXP.errhigh_B15=[0.051 0.044 0.032 0.022 0.021 0.019 0.017 0.012 NaN 0.009]; %concentration
EXP.errlow_B15=[0.048 0.028 0.022 0.02 0.019 0.015 0.012 0.012 NaN 0.007]; %concentration
%plots
figure(1)
%Blood Pharmacokinetics of 15E-03, 183E-03, 1058E-03 mg/g dose GSH-AuNPs
set(gcf, 'Position',[0,45,1800,900])
set(0,'defaultAxesFontName', 'Timesnewroman')
set(0,'defaultTextFontName', 'Timesnewroman')
ylabel(['%ID/g in blood(mg/g)'])%CF of (%ID/g) = mg/g
subtightplot(2,2,1, margins)
bar(EXP.t,EXP.Blood_15)
set(gca,'fontsize',12,'FontName','arial','XScale','lin','Xlim',[0,24],'XTick',[0,6,12,18,24]);
hold on
ylabel(['GSH-AuNPs conc. in blood(mg/g)'])
xlabel(['Time(h)'])
plot(t1,(m_Au_V1+m_Au_A1),'-',EXP.t,EXP.Blood_15,'o','Color', blue,'LineWidth',2)
er = errorbar(EXP.t,EXP.Blood_15,EXP.errlow_B15,EXP.errhigh_B15);
er.Color = [0 0 0];
er.LineStyle = 'none';
lgd= legend('simulated data','experimental data','Location','northeast');
title(lgd,'15E-03 mg/g ID')
hold off
I tried to plot the error bar but shows the error:
Error using bar (line 182)
XData values must be unique.
Error in para_vald_GSH (line 342)
bar(EXP.t,EXP.Blood_15)
So how can I plot error bars for multiple time points at a single time point? Please help me. It will be appreciated. Thank you in advance.
  1 Comment
the cyclist
the cyclist on 28 Jun 2021
The problem is not with the error bars, but with the bar chart that precedes that plotting.
Let's simplify a little, and look only at the first few points. What is your expectation about what this code's output should look like?
EXP.t=[0 0 0 0 2.16]; %hours
EXP.Blood_15=[0.05 0.036 0.027 0.021 0.02]; %concentration
%plots
bar(EXP.t,EXP.Blood_15)
Error using bar (line 182)
XData values must be unique.
Because to me, this would create (if MATLAB allowed it) several bars at X = 0, which is difficult to understand/interpret.

Sign in to comment.

Answers (1)

dpb
dpb on 28 Jun 2021
Plot versus ordinal value and then use xticklabels to label the times...
hB=bar(EXP.Blood_15);
hold on
hEr=errorbar(1:numel(EXP.Blood_15),EXP.Blood_15,EXP.errlow_B15,EXP.errhigh_B15,'k','LineStyle','none');
xticklabels(compose('%.2f',EXP.t))
xlabel('Time (hr)')
ylabel('GSH-AuNPs conc. in blood(mg/g)')
yields
Salt to suit; not sure about why the funky-looking y-label text...
If you try to plot() or bar() with actual t values repeated on the axis, then there's nothing to separate the four time zero cases--although I've no idea what this is/represents, those look somewhat peculiar in a decreasing pattern as is -- I presume probably the time isn't actually identically zero but something less than an hour but not recorded, maybe?
  14 Comments
dpb
dpb on 5 Jul 2021
Edited: dpb on 5 Jul 2021
Well, taking the mean of a vector is trivial; it's not clear what that mean would be representative of in your case if it is one of the time histories.
If you mean a "vector" as the collection of time histories at a given time, then MATLAB will deal with that for you automagically as mean() is vectorized to operate by column by default over a 2D array or, with the optional DIM input argument, it will compute the means of each row (or whatever dimension is given for higher dimensionality arrays).
Example w/ dummy data array--
>> A=rand(4,3); % 4 rows, 3 columns
>> [mean(A); std(A)] % by default operates by column
ans =
0.3914 0.4985 0.3898
0.3439 0.1605 0.4082
>>
So, the above gives the mean and std for each column of the array -- if the time histories are by row for each of the 10 histories and each column is a time step, this is what you want.
>> [mean(A,2), std(A,[],2)] % instead go by row instead of by column
ans =
0.5097 0.3232
0.1475 0.1437
0.4647 0.1904
0.5844 0.3916
>>
If, instead each column represents a time series and the rows are the time steps, then the above computes the statistics again over the collection of time series.
I don't know what more to tell you...
Rajvi Amle
Rajvi Amle on 7 Jul 2021
Thank you for your efforts and time @dpb. Further, in my code, when I tried to plot the simulated and experimental data together, they were not much closer to each other so I tried to optimize the parameter values and used 'fmincon' function. However I am getting the error when the code is run. I tried to solve it but wasn't able to overcome it. Could you please have a look and help me with this? Following is the part of my code where fmincon function was used:
%Initial and boundary conditions for optimization
% Optimization variables - Initial conditions, lower and upper bounds
ov_ini(1) = 4.338; lb_1=0; ub_1=ov_ini(1)*2; % 1.85 K_max_L [h-1]
ov_ini(2) = 233.06; lb_2=0; ub_2=ov_ini(2)*2; % 4.06 K_max_S [h-1]
ov_ini(3) = 0.804; lb_3=0; ub_3=ov_ini(3)*2; % 0.0192 K_max_K [h-1]
ov_ini(4) = 0.173; lb_4=0; ub_4=ov_ini(4)*2; % 0.0643 K_max_Lu [h-1]
ov_ini(5) = 5.4E-05; lb_5=0; ub_5=1; % 0.08 PL [-]
ov_ini(6) = 0.0447; lb_6=0; ub_6=1; % 0.15 PBR [-]
ov_ini(7) = 0.3435; lb_7=0; ub_7=1; % 0.15 PK [-]
ov_ini(8) = 0.6327; lb_8=0; ub_8=1; % 0.15 PS [-]
ov_ini(9) = 0.0095; lb_9=0; ub_9=1; % 0.15 PLu [-]
ov_ini(10) = 4.9E-06; lb_10=0; ub_10=1; % 0.15 PRes [-]
ov_ini(11) = 0.00099304; lb_11=0; ub_11=1; % 0.001 PALC [-]
ov_ini(12) = 7.04E-06; lb_12=0; ub_12=ov_ini(12)*2; % 0.001 PASC [-]
ov_ini(13) = 2.6E-04; lb_13=0; ub_13=ov_ini(13)*2; % 0.00034 PAKC [-]
ov_ini(14) = 8.13E-06; lb_14=0; ub_14=ov_ini(14)*2; % 0.001 PALuC [-]
ov_ini(15) = 0.01; lb_15=0; ub_15=ov_ini(15)*2; % 0.000001 PABRC [-]
ov_ini(16) = 0.01; lb_16=0; ub_16=ov_ini(16)*2; % 0.000001 PAResC [-]
ov_ini(17) = 3.3E-04; lb_17=0; ub_17=ov_ini(17)*2; % 0.0075/3 K_rel_L [h-1]
ov_ini(18) = 0.0022; lb_18=0; ub_18=ov_ini(18)*2; % 0.003/3 K_rel_S [h-1]
ov_ini(19) = 2.03E-05; lb_19=0; ub_19=ov_ini(19)*2; % 0.000385 K_rel_K [h-1]
ov_ini(20) = 0.0043; lb_20=0; ub_20=ov_ini(20)*2; % 0.005/10 K_rel_Lu [h-1]
lb = [lb_1 lb_2 lb_3 lb_4 lb_5 lb_6 lb_7 lb_8 lb_9 lb_10 lb_11 lb_12 lb_13 lb_14 lb_15 lb_16 lb_17 lb_18 lb_19 lb_20];% lower boundary
ub = [ub_1 ub_2 ub_3 ub_4 ub_5 ub_6 ub_7 ub_8 ub_9 ub_10 ub_11 ub_12 ub_13 ub_14 ub_15 ub_16 ub_17 ub_18 ub_19 ub_20];% upper boundary
ov0 = [ov_ini];
%% Parameter Optimization
MAT=[SYSTEM.m_BW(1) SYSTEM.w_L(1) SYSTEM.w_K(1) SYSTEM.w_Lu(1) SYSTEM.w_BR(1) SYSTEM.w_Blood(1) SYSTEM.w_Plasma(1) SYSTEM.m_L(1) SYSTEM.m_BR(1) SYSTEM.m_K(1) SYSTEM.m_S(1) SYSTEM.m_Lu(1) SYSTEM.m_Blood(1) SYSTEM.m_Plasma(1)...
SYSTEM.V_L(1) SYSTEM.V_BR(1) SYSTEM.V_K(1) SYSTEM.V_S(1) SYSTEM.V_Lu(1) SYSTEM.V_Blood(1) SYSTEM.V_Plasma(1) SYSTEM.F_C(1) SYSTEM.F_L(1) SYSTEM.F_BR(1) SYSTEM.F_K(1) SYSTEM.F_S(1) SYSTEM.F_Res(1) SYSTEM.F_bal(1) SYSTEM.F_Bile(1) SYSTEM.F_Urine(1)...
SYSTEM.V_Res(1) SYSTEM.V_bal(1) SYSTEM.V_L_b(1) SYSTEM.V_L_t(1) SYSTEM.V_BR_b(1) SYSTEM.V_BR_t(1) SYSTEM.V_K_b(1) SYSTEM.V_K_t(1) SYSTEM.V_S_b(1) SYSTEM.V_S_t(1) SYSTEM.V_Lu_b(1) SYSTEM.V_Lu_t(1) SYSTEM.V_Res_b(1) SYSTEM.V_Res_t(1)...
DRUG.m_Au_iv(1) DRUG.M_Au_iv(1)]';
opt = optimset('MaxFunEval',1E5,'Maxiter',1E3,'Algorithm','interior-point',...
'TolX',1E-5,'TolFun',1E-6,'Display','iter');
[val_par, fval, v3, v4, v5, v6, H] = ...
fmincon(@obfun_chitosan,ov0,[],[],[],[],lb,ub,[],opt,SYSTEM,DRUG,CF,EXP,MAT);
%% Solver
% ov = val_par(1:20);
% ov = ov0;
ov_val = ov';
save('val_para','ov_val')
for i=1
if i == 1
%Dosing
MAT=[SYSTEM.m_BW(1) SYSTEM.w_L(1) SYSTEM.w_K(1) SYSTEM.w_Lu(1) SYSTEM.w_BR(1) SYSTEM.w_Blood(1) SYSTEM.w_Plasma(1) SYSTEM.m_L(1) SYSTEM.m_BR(1) SYSTEM.m_K(1) SYSTEM.m_S(1) SYSTEM.m_Lu(1) SYSTEM.m_Blood(1) SYSTEM.m_Plasma(1)...
SYSTEM.V_L(1) SYSTEM.V_BR(1) SYSTEM.V_K(1) SYSTEM.V_S(1) SYSTEM.V_Lu(1) SYSTEM.V_Blood(1) SYSTEM.V_Plasma(1) SYSTEM.F_C(1) SYSTEM.F_L(1) SYSTEM.F_BR(1) SYSTEM.F_K(1) SYSTEM.F_S(1) SYSTEM.F_Res(1) SYSTEM.F_bal(1) SYSTEM.F_Bile(1) SYSTEM.F_Urine(1)...
SYSTEM.V_Res(1) SYSTEM.V_bal(1) SYSTEM.V_L_b(1) SYSTEM.V_L_t(1) SYSTEM.V_BR_b(1) SYSTEM.V_BR_t(1) SYSTEM.V_K_b(1) SYSTEM.V_K_t(1) SYSTEM.V_S_b(1) SYSTEM.V_S_t(1) SYSTEM.V_Lu_b(1) SYSTEM.V_Lu_t(1) SYSTEM.V_Res_b(1) SYSTEM.V_Res_t(1)...
DRUG.m_Au_iv(1) DRUG.M_Au_iv(1)]';
tspan = [0 max(EXP.t)];
options=odeset('AbsTol',10e-2,'RelTol',10e-2,'Stats','on');
x0=zeros(19,1);
tic;
[t1,X1] = ode15s(@ode_chitosan, tspan,x0, [], SYSTEM,DRUG, CF, ov, MAT);
toc;
end
function out = obfun_chitosan(ov,SYSTEM,DRUG,CF,EXP,MAT)
for i=1
%% Initial conditions and Input variables
if i == 1
% Initial conditions for ODEs
x0=zeros(19,1);
end
%% Solver
ov = ov(1:20);
if i == 1
[t1,X1] = ode15s(@ode_chitosan, EXP.t, x0,[], SYSTEM,DRUG, CF, ov,MAT);
m_Au_A1=X1(:,1); % Amount of AuNP in arterial blood
m_Au_V1=X1(:,2); % Amount of AuNP in venous blood
m_Au_Lu_b1=X1(:,3); % Amount of AuNP in lung capillary blood
m_Au_Lu_t1=X1(:,4); % Amount of AuNP in lung tissue
m_Au_Lu_PC1=X1(:,5); % Amount of AuNP in lung PC
m_Au_BR_b1=X1(:,6); % Amount of AuNP in brain capillary blood
m_Au_BR_t1=X1(:,7); % Amount of AuNP in brain tissue
m_Au_Res_b1=X1(:,8); % Amount of AuNP in rest of body capillary blood
m_Au_Res_t1=X1(:,9); % Amount of AuNP in rest of body tissue
m_Au_K_b1=X1(:,10); % Amount of AuNP in kidney capillary blood
m_Au_K_t1=X1(:,11); % Amount of AuNP in kidney tissue
m_Au_K_PC1=X1(:,12); % Amount of AuNP in kidney PC
m_Au_S_b1=X1(:,13); % Amount of AuNP in spleen capillary blood
m_Au_S_t1=X1(:,14); % Amount of AuNP in spleen tissue
m_Au_S_PC1=X1(:,15); % Amount of AuNP in spleen PC
m_Au_L_b1=X1(:,16); % Amount of AuNP in liver capillary blood
m_Au_L_t1=X1(:,17); % Amount of AuNP in liver tissue
m_Au_L_PC1=X1(:,18); % Amount of AuNP in liver PC
AUC1=X1(:,19);
for k = 1:length(m_Au_A1)
h = ae_chitosan(X1(k,:),SYSTEM,DRUG,CF,ov,t1(k),MAT);
M_Au_iv1(k)=h.M_Au_iv;
rho_Au_A1(k)=h.rho_Au_A;
rho_Au_V1(k)=h.rho_Au_V;
rho_Au_Lu1(k)=h.rho_Au_Lu;
rho_Au_L1(k)=h.rho_Au_L;
rho_Au_K1(k)=h.rho_Au_K;
rho_Au_S1(k)=h.rho_Au_S;
m_Au_Lu_total1(k)=h.m_Au_Lu_total;
m_Au_L_total1(k)=h.m_Au_L_total;
m_Au_K_total1(k)=h.m_Au_K_total;
m_Au_S_total1(k)=h.m_Au_S_total;
w_Au_Kidney_Tissue1(k)=h.w_Au_Kidney_Tissue;
w_Au_Spleen_Tissue1(k)=h.w_Au_Spleen_Tissue;
w_Au_Liver_Tissue1(k)=h.w_Au_Liver_Tissue;
w_Au_Lung_Tissue1(k)=h.w_Au_Lung_Tissue;
w_Au_Brain_Tissue1(k)=h.w_Au_Brain_Tissue;
w_Au_Plasma1(k)=h.w_Au_Plasma;
end
end
end
%% Objective Function - Minimizing sum of squared residuals
%
out = (sum((abs(EXP.Blood_C' - (rho_Au_A1+rho_Au_V1))/max(EXP.Blood_C)).^2));
The error shown is:
Error using fmincon (line 639)
Supplied objective function must return a scalar value.
Error in para_vald_chitosan (line 119)
fmincon(@obfun_chitosan,ov0,[],[],[],[],lb,ub,[],opt,SYSTEM,DRUG,CF,EXP,MAT);
the size of each input is:
ov0= 20x1
lb=1x20
ub=1x20
SYSTEM=1x1 struct
DRUG=1x1 struct
CF=1x1 struct
EXP=1x1 struct
MAT=46x1

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!