Is it possible to skip for loop when error appears and replace the results with NaN?

조회 수: 3 (최근 30일)
I have a 3D matrix ( Auto) and in which I am fitting the Auto(p,q,:) elements using a for a loop. I am calculating one parameter ( D(p,q)=fit1.D) from the fitting. However, due to some reason ( Complex value computed by model function, fitting cannot continue), fitting is not happening for some p,q and hence the for loop terminated. Here I want to put D(p,q)=NaN when the fitting gives the error and want to proceed for next loop.
Thanks in advance for your help.
l2= size(Auto);
for p=1:l2(1)
for q=1:l2(2)
y=reshape(Auto(p,q,2:end),[],1);
x=(0:0.5:(35-1)*0.5)';
f = fittype('(A./(sqrt(1+ ((x*D)./(0.251.^2)))))');
fit1 = fit(x,y,f,'StartPoint',[0.1 0.1 ]);
D(p,q)=fit1.D;
% D(p,q)=NaN;
end
end

채택된 답변

James Tursa
James Tursa 2018년 7월 11일
Something like this?
try
% your fitting stuff goes here
D(p,q) = fit1.D;
catch
D(p,q) = NaN;
end
  댓글 수: 1
Jaladhar Mahato
Jaladhar Mahato 2018년 7월 11일
Thanks, James for your quick reply.
It is indeed a great trick. I have faced this many times but somehow I overcome this. I think it will help a lot of other users. The only problem with my data is that in most of the cases fitting is not happening (Which becomes vivid from a lot of NaN values in the output). Thanks again.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by