How to terminate for-loop and use current loop value if warning message appears?
Mostrar comentarios más antiguos
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 comentarios
In terms of simply terminating the loop if you get no error, just putting
break;
at the end of the try block should achieve this, unless I am misunderstanding some of the complexity.
Telling the difference between warnings and errors programmatically I'm not so sure about though. You can (and it is often good practice to do so - otherwise you can end up catching syntax errors, for example, too and doing whatever is in the catch block for these) add a variable after the catch, i.e.
catch me
so that in the catch block you can interrogate that variable (which I called me because the object is an MException object, btw! Obviously you can call it whatever you wish.).
So you can test for specific error identifiers there, which is common - e.g. if it is a specific type of error then do something, otherwise rethrow the error (this is what I usually do - I expect certain error identifiers to be thrown, but if I get some other unexpected error then I want to rethrow that and have it still produce an error rather than catch it). I don't see any simple way to query whether it is an error or a warning though...I'll take a little longer look.
doc MException
will give you the details of what you can do with that object though.
I would suggest that if you expect only certain types of errors that you use this approach to catch the specific ones.
If you don't know what the identifier is for an error you can always find it either by adding that me variable, putting in a breakpoint and looking at
me.identifier
or
doc lasterror
is also useful for you to get information on the last error you had.
dpb
el 28 de Feb. de 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ß
el 28 de Feb. de 2020
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!