fitting data form a matrix using loops

조회 수: 2 (최근 30일)
federico drudi
federico drudi 2022년 9월 19일
댓글: federico drudi 2022년 9월 20일
hello,
I made the following chart with averages, error bars and fittings starting from my data set. It took me some time as I am quite new to Matlab, but I know I did it wrong. I should be using loops but I have tried several times with no success.
Can someone please help me to understand it better?
Thank you very much!!!
d=readmatrix("shear_Stress_more.csv");
Error using readmatrix
Unable to find or open 'shear_Stress_more.csv'. Check the path and filename or file permissions.
ctrl = mean(d(:,2:5),2);
pef = mean(d(:,6:9),2);
USa = mean(d(:,10:11),2);
USb = mean(d(:,12:13),2);
enz = mean(d(:,14:17),2);
medie=[ctrl pef USa USb enz];
st_ctrl = std(d(:,2:5),0,2);
st_pef = std(d(:,6:9),0,2);
st_USa = std(d(:,10:11),0,2);
st_USb = std(d(:,12:13),0,2);
st_enz = std(d(:,14:17),0,2);
stdev = [st_ctrl st_pef st_USa st_USb st_enz];
ft = fittype('a+(b-a)*exp(-c*x)', ...
'dependent',{'y'}, ...
'independent',{'x'}, ...
'coefficients',{'a','b','c',});
hold on
[f,gof] = fit(d(:,1),medie(:,1), ft,'StartPoint',[3, 2, 0.01]);
errorbar(d(:,1),medie(:,1),stdev(:,1));
plot(f);
[f2,gof2] = fit(d(:,1),medie(:,2), ft,'StartPoint',[3, 2, 0.01]);
errorbar(d(:,1),medie(:,2),stdev(:,2));
plot(f2);
[f3,gof3] = fit(d(:,1),medie(:,3), ft,'StartPoint',[3, 2, 0.01]);
errorbar(d(:,1),medie(:,3),stdev(:,3));
plot(f3);
[f4,gof4] = fit(d(:,1),medie(:,4), ft,'StartPoint',[3, 2, 0.01]);
errorbar(d(:,1),medie(:,4),stdev(:,4));
plot(f4);
[f5,gof5] = fit(d(:,1),medie(:,5), ft,'StartPoint',[3, 2, 0.01]);
errorbar(d(:,1),medie(:,5),stdev(:,5));
plot(f5);
xlabel({'Time t [s]'});
ylabel({'Shear stress σ [Pa]'});
title({''});
legend('Control','PEF','USa','USb','Enzyme');
hold off

채택된 답변

Walter Roberson
Walter Roberson 2022년 9월 19일
You do not need all those variables at the same time, not for the purpose of the plot. You could look
for K = 1 : 5
start = (K-1)*4 + 2;
stop = start + 3;
thisdata = data(:,start:stop);
m = mean(thisdata,2);
s = std(thisdata);
[f, gof] = fit(d(:,1), m, ft, 'StartPoint', [3, 2, 0.01]);
errorbar(d(:,1), m, s);
h(K) = plot(f);
hold on
end
hold off
xlabel({'Time t [s]'});
ylabel({'Shear stress σ [Pa]'});
title({''});
legend(h, {'Control','PEF','USa','USb','Enzyme'});
  댓글 수: 1
federico drudi
federico drudi 2022년 9월 20일
Thanks a lot, after a few changes it works perfectly and most importantly I am starting to understand this looping thing, really helpful.
Thanks again!
Cheers!!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2022년 9월 19일
No real deal...your first part is somewhat more of a pain since you're averaging over a variable number of columns -- if it's a one-time exercise, I'd probably basically leave it along and only deal with it further if have to change these column IDs manuall
d=readmatrix("shear_Stress_more.csv");
% build summary data arrays
davg=[mean(d(:,2:5),2) mean(d(:,6:9),2) mean(d(:,10:11),2) mean(d(:,12:13),2) mean(d(:,14:17),2)];
dstd=[std(d(:,2:5),2) std(d(:,6:9),2) std(d(:,10:11),2) std(d(:,12:13),2) std(d(:,14:17),2)];
ft = fittype('a+(b-a)*exp(-c*x)', ...
'dependent',{'y'}, ...
'independent',{'x'}, ...
'coefficients',{'a','b','c',});
hold on
% build fit across columns
for i=1:size(davg,2);
[f{i},gof{i}] = fit(d(:,1),davg(:,i),ft,'StartPoint',[3, 2, 0.01]);
errorbar(d(:,1),davg(:,i),dstd(:,i));
plot(f{i})
end
xlabel({'Time t [s]'});
ylabel({'Shear stress σ [Pa]'});
title({''});
legend('Control','PEF','USa','USb','Enzyme');
hold off

Community Treasure Hunt

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

Start Hunting!

Translated by