Save data when an error occurs inside a function.

Hi,
I have an algorithm (called algorithm A) that takes hours to run. Algorithm A is written in a script file 'Algorithm_A.m'.
I will run alogirthm A in various cases, to make it easy for me to organize my code, I want to rewrite the script file 'Algorithm_A.m' as a function.
The thing I am concerned is that if I rewrite A into a function, will be it possible to debug the code if some error occurs inside this function?
A takes hours to run so it is important for me to check the data at the time where the error occurs.
Is it possible?
If it is, then could you tell me how to do it? How to save the data at the time where an error occurs?
Thanks !

댓글 수: 3

The most useful debugging command is
dbstop if error
With this you do not need to set any breakpoints or even know where an error might occur. Just run that command, then run your code: it will stop on the line where any error occurs, and you have immediate access to all of the variables in the function workspace.
Thanks Stephen.
Will it work if I run my code on a shared computational cluster?
"Will it work if I run my code on a shared computational cluster? "
I do not know. If it does not work, then probably the try-catch solution is your best choice.

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

 채택된 답변

Steven Lord
Steven Lord 2021년 4월 2일

0 개 추천

Either set an error breakpoint before running the code or use a try / catch block. The error breakpoint will let you debug the code but will stop execution at the point of the error, which may not be what you want if you're running this code repeatedly and want to move on to the next iteration and come back later to determine what happened. The try / catch block will let you do whatever cleanup (saving the data, closing figures or files, etc.) you need to do before continuing on but won't let you debug the code from the point at which it failed.

댓글 수: 5

Thanks Steven.
So, using try/catch block inside the function, it will save the workspace of the function if there is an error occurs inside it, right?
It is ok if I cannot debug but it is important to be able to check variables in the workspace of the function (NOT THE GLOBAL WORKSPACE) as my code will be run on cluseter/supercomputer (that's why error breakpoint will not help in my case).
Inside the catch part of the try / catch block you could call save, yes.If you write your catch block correctly you could even get information about what error was thrown and from where.
try
A = magic(4)+magic(5); % Can't add a 4-by-4 and a 5-by-5 matrix
disp(A)
catch ME
disp(ME)
end
MException with properties: identifier: 'MATLAB:sizeDimensionsMustMatch' message: 'Arrays have incompatible sizes for this operation.' cause: {} stack: [3×1 struct] Correction: []
disp("This statment still runs even though there was an error")
This statment still runs even though there was an error
In your example, I still cannot see where the error was from.
I really want to see what the error was, from which function at which line... Basically just like an error message when I run Matlab without using try/catch.
What should I do to achieve that?
Thanks.
Look at the contents of the stack property of the MException object named ME in the try / catch block.
Thanks. I will try.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Exception Handling에 대해 자세히 알아보기

질문:

2021년 4월 2일

댓글:

2021년 4월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by