function exampleFun
cleanupObj = onCleanup(@()myCleanupFun);
for n=1:5
pause(1)
disp(".")
end
cleanupObj.cancel
disp("Function completed successfully")
endfunction myCleanupFun
disp("Function was canceled prematurely")
end
运行 exampleFun。
exampleFun
.
.
.
.
.
Function completed successfully
再次运行 exampleFun,这次在函数运行时使用 Ctrl+C 或 Ctrl+Break 取消执行。在 Apple Macintosh 平台上,还可以使用 Command+.(Command 键和句点键)。
exampleFun
.
.
.
Function was canceled prematurely
Operation terminated by user during exampleFun (line 4)