How to terminate for-loop and use current loop value if warning message appears?

1 Ansicht (letzte 30 Tage)
Hi everyone,
I am encountering a problem while using the fitgmdist function. In case an "error" appears, I want to pass on the for-loop to the next seed_start value, but if it is merely a "warning" and thus the code could be executed and actually fit the Gaussian, then I want to use the current seed_start value and terminate the for-loop.
Unfortunately, if Matlab encounters a warning message, then it treats it the same as an error and passes it on to the next seed_start value.
I have tried turning off warnings but I cannot solve it this way (not displayed in code below).
Is there any way I can terminate the loop in the catch statement?
Thank you!
AIC=zeros(1,5); %test for Gaussian mixture distributions from 1 to 5 components (components correspond to speakers)
gm=cell(1,5);
options = statset('MaxIter',10000);
for seed_start=1:101 %if a GMM doesn't succeed because of an ill-conditioned covariance
% created at a certain iteration, then use another seed value for the fitgmdist function to act on
if seed_start==1
try
rng 'default' %for reproducibility
for k=1:5
gm{k}=fitgmdist(X,k,'Options',options,'CovarianceType','diagonal');
AIC(k)=gm{k}.AIC;
end
catch
end
else
try
rng(seed_start-1) %use other initial values
for k=1:5
gm{k}=fitgmdist(X,k,'Options',options,'CovarianceType','diagonal');
AIC(k)=gm{k}.AIC;
end
catch
end
end
end
  3 Kommentare
dpb
dpb am 28 Feb. 2020
Read up on exception handling in Matlab. Add
catch ME
to the above code and then inspect the exception object inside the catch clause. You can do whatever you wish within it before either terminating or taking some fixup and rethrowing the error.
Sebastian Groß
Sebastian Groß am 28 Feb. 2020
Indeed it was the missing break command at the end of the try block which solved the issue. Now warnings are ignored and end the for-loop and errors pass on to the next for-loop value!
Thank you so much!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sebastian Groß
Sebastian Groß am 28 Feb. 2020
Inserting break at the end of try block finishes the job. Thanks!

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by