Cleanup tasks upon function completion
cleanupObj = onCleanup(cleanupFun)
cleanupObj = onCleanup(
creates
an object that, when destroyed, executes the function cleanupFun
)cleanupFun
. MATLAB® implicitly
clears all local variables at the termination of a function, whether
by normal completion, or a forced exit, such as an error, or Ctrl+C.
If you reference or pass cleanupObj
outside
your function, then cleanupFun
does not run when
that function terminates. Instead, it runs whenever MATLAB destroys
the object.