필터 지우기
필터 지우기

How to store extracted coefficient values from curve fitting of multiple .txt files in index arrays?

조회 수: 1 (최근 30일)
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];

채택된 답변

Matt J
Matt J 2023년 11월 15일
편집: Matt J 2023년 11월 15일
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
Mike
Mike 2023년 11월 16일
Thanks a lot. It worked for me. How do I sort which 'i' th coeffvalues refers to which txt file?
Matt J
Matt J 2023년 11월 16일
They will be in the same order that they were processed in the loop.

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

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 11월 15일
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

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by