Hey Matlab friends, I have to estimate my 4 parameters, for 200 .matfiles and store the results. I created the codes below but it works for the first 6 datasets.

3 views (last 30 days)
files = dir('*.mat') ; % all the .mat files in the folder
N = length(files) ; % Total number of files
for LL=1:N
thisfile = files(LL).name ;
load(thisfile)
DV=T.dv;
lengthTable=length(DV);
ID=T.ID;
time=T.time;
dose=T.amt;
DVnew=[];
timeNew=[];
doseNew=[];
IDnew=[];
lengthTable=length(DV);
k=1;j=1;
for i=1:lengthTable
if ~isnan(dose(i))
doseperid(k)=dose(i);
k=k+1;
else
if ~isnan(time(i))&&(~isnan(DV(i)))&&(~isnan(ID(i)))
doseNew(j,:)=doseperid(k-1);
timeNew(j,:)=time(i);
DVnew(j,:)=DV(i);
IDnew(j,:)=ID(i);
j=j+1;
end
end
end
dosePerID=doseperid';
%% SSEMMpk with ode45
logparameter0=log([1 1 0.2 0.5 ]);
model = @Getsolutionode;
n=length(dosePerID);
%% SSEMMpk with ode45
P=[1 0 0 0 ; 0 1 0 0; 0 0 1 0; 0 0 0 1];
%% nlmefit
[beta,PSI,stats,b] = nlmefit(timeNew,DVnew,IDnew,...
dosePerID,model,logparameter0,...
'REParamsSelect',[1 2 3 4 ],'ErrorModel','Constant','ApproximationType','FOCE','CovPattern',P,'RefineBeta0','on',...
'ParamTransform',repelem(1,4),'OptimFun','fminunc');
%% nlmefir output
beta2=beta;
b2=b;
bb=exp(beta);
psi=PSI;
STATS=stats;
STATS.COVB=stats.covb;
parameter=exp(beta');
predictor=timeNew;
%% storing all the dataset outputs
Beta{:,LL}=beta2;
B{LL}=b2;
A2{:,LL}=bb;
psi2{:,LL}=psi;
STATS2{:,LL}=STATS;
STATS.COVB2{:,LL}=STATS.COVB;
end
function concentration=Getsolutionode(parameter,t,doseNew)
%% initial values
ka=parameter(:,1);
v=parameter(:,2);
vmax=parameter(:,3);
km=parameter(:,4);
predictor(:,1)=t;
minTime=0;
maxTime=max(t);
y0=[doseNew 0];
%% ode45
options=odeset('RelTol',1e-6);
solode=ode45(@odefunction,[minTime maxTime], [y0],options,ka,v,vmax,km);
rspl=deval(solode,t);
concentration=rspl(2,:)'./v;
end
  5 Comments
Stephen23
Stephen23 on 7 Nov 2021
" it faced with error after 6 loops!"
Please show us the complete error message. This means all of the red text.
Sepi Sharif
Sepi Sharif on 7 Nov 2021
update:
I have changed the first two lines of loading
files = dir('*.mat') ; % all the .mat files in the folder
N = length(files) ; % Total number of files
to:
files = dir('*.mat') ; % all the text files in the folder
N = length(files) ;
the error :
Error using nlmefit>checkFunVals (line 1858)
MYFUN has returned Inf or NaN values.
Error in nlmefit>GNfitPNLS (line 1267)
if funValCheck && ~isfinite(sse_new), checkFunVals(sse_new); end
Error in nlmefit (line 601)
[BetaNew,bNew,stop] = GNfitPNLS(Delta,Y,Beta,b,weights,PNLSOpt,grp,grpMatch,M,N,r,q,computeFAllGroupsFcn,computeJacobianAllGroupsFcn);
Error in MMpk_IndLin_Test (line 80)
[beta,PSI,stats,b] = nlmefit(timeNew,DVnew,IDnew,...

Sign in to comment.

Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!