When an error occurs how I can restar the mfile from a specific line?

1 次查看(过去 30 天)
Hi there,
I'm using the Optimization Toolbox with the Genetic Algorithm (GA) in order to optimize the discretization of a continuous variable. When the funcion runs happens that if the discretization, generated by the GA, is bad the software get an error and stops. I want to find a way to jump the error and allow the GA to execute until to finding the optimum discretization, so:
if generic error
restart from line xx
Thanks

回答(2 个)

Matt J
Matt J 2013-1-19
Use TRY and CATCH at the line where the error occurs.

Image Analyst
Image Analyst 2013-1-19
As far as I know, there is no way, short of putting every single line of code inside the "try" block of a try/catch construct, which is obviously not practical except for the shortest script where you can just step though with the debugger anyway.
There is no "resume next" as there is in Visual Basic. In Visual Basic you can put "resume next" in the catch block and it will return and pick up at the line right after where the exception occurred. Unfortunately no such capability in MATLAB though on a few occasions I have wished for it. You just have to rearrange your code to (as much as possible) put as much of the code that is crucial to get executed near the top of the routine, and error prone code (like checking if some kind of hardware or device is attached to your computer) near the end of the routine.

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by