Close diary file on Error
Mostrar comentarios más antiguos
Hi, I am using the diary function to log my application output. Something like
diary(path_to_logfile)
% all my code with output to the console, matrices written to desk, etc
diary off
When the code fails, the diary is not closed. One option would be to wrap all the code on a try-catch statement and close it on the catch.
Is there another way to avoid the diary being "open" when the app fails?
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 20 de Jul. de 2012
3 votos
onCleanup() might be appropriate for you.
Andrew Janke
el 31 de En. de 2020
The onCleanup function is what you want in modern Matlab. No try/catch to ugly up your code, and it's robust even against a dbquit.
function my_function
diary(path_to_logfile)
RAII.diary = onCleanup(@() diary('off'))
% ... now do whatever, and don't worry about closing the diary; it'll
% be automatically closed whenever this function returns for any reason...
end
2 comentarios
Florian Rössing
el 6 de Mayo de 2022
Super usefull, thank you
Tria Technologies
el 29 de En. de 2025
Upvoting the use of onCleanup I recently discovered this and it has added better predictability to functions. Of course, classes have built in delete() methods for doing the same.
Categorías
Más información sobre Scope Variables and Generate Names 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!