How to store extracted coefficient values from curve fitting of multiple .txt files in index arrays?
1 view (last 30 days)
Show older comments
Hi Everyone,
I am doing curve fitting on multiple text files having same input except one. I am using fittype to fit an equation to the experimental data. I have created initial arrays to store extracted coefficients from the curve fitting. After I run code I see the extracted coefficient values of only one .txt file. I am looking for a code that will allow all txt file extracted parameters to be saved in initial arrays. Here is code:
f = fittype( 'C1+C4*(C5^2/(C5^2+(x-C6)^2))+C7*(C5*(x-C6)/(C5^2+(x-C6)^2))+C8*x');
C11=[];
C41=[];
C51=[];
C61=[];
C71=[];
[fit1,gof] = fit(xdata,ydata,f,'StartPoint',[1 -2 20 150 2 2], 'Lower', [-Inf -Inf -Inf -Inf -Inf -Inf ], 'Upper',[Inf Inf Inf Inf Inf Inf], 'Robust','On');
c1=fit1.C1;
c4=fit1.C4;
c5=fit1.C5;
c6=fit1.C6;
c7=fit1.C7;
c8=fit1.C8;
C=[c4,c5,c6,c7];
0 Comments
Accepted Answer
Matt J
on 15 Nov 2023
Edited: Matt J
on 15 Nov 2023
f = fittype( 'C1+C4*(C5^2/(C5^2+(x-C6)^2))+C7*(C5*(x-C6)/(C5^2+(x-C6)^2))+C8*x');
C=nan(5,numFiles); %pre-allocate
for i=1:numFiles
xdata=...read
ydata=...read
[fit1,gof] = fit(xdata,ydata,f,'StartPoint',[1 -2 20 150 2 2], 'Lower', [-Inf -Inf -Inf -Inf -Inf -Inf ], 'Upper',[Inf Inf Inf Inf Inf Inf], 'Robust','On');
C(:,i)=coeffvalues(fit1);
end
2 Comments
More Answers (1)
Sulaymon Eshkabilov
on 15 Nov 2023
It can be done something like this way:
Folder = 'C:\Users\...'; % Directory where the files are residing
F_Pat = fullfile(Folder, '*.txt');
FILES = dir(F_Pat);
F_Names={FILES.name};
f = fittype( 'C1+C4*(C5^2/(C5^2+(x-C6)^2))+C7*(C5*(x-C6)/(C5^2+(x-C6)^2))+C8*x');
C11=[];
C41=[];
C51=[];
C61=[];
C71=[];
C81 =[];
for k = 1 :length(F_Names)
Get_FName = FILES(k).name;
F_FileName=fullfile(FILES(k).folder, Get_FName);
D= readmatrix(F_FileName);
xdata=D(:,1);
ydata=D(:,2);
[fit1,gof] = fit(xdata,ydata,f,'StartPoint',[1 -2 20 150 2 2], 'Lower', [-Inf -Inf -Inf -Inf -Inf -Inf ], 'Upper',[Inf Inf Inf Inf Inf Inf], 'Robust','On');
c1=fit1.C1;
c4=fit1.C4;
c5=fit1.C5;
c6=fit1.C6;
c7=fit1.C7;
c8=fit1.C8;
C=[c4,c5,c6,c7];
C11=[C11, c1];
C41=[C41, c4];
C51=[C51, c5];
C61=[C61, c6];
C71=[C71, c7];
C81=[C81, c8];
end
0 Comments
See Also
Categories
Find more on Whos 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!