主要内容

cancel

取消清理任务

自 R2025a 起

    说明

    cancel(cleanupObj) 取消由 onCleanup 对象指定的任何清理任务,并防止它们在 cleanupObj 被销毁时执行。

    示例

    示例

    全部折叠

    创建一个函数,其中包含 onCleanup 对象,该对象在被销毁时显示一条消息,但当函数成功执行时取消显示该消息。

    function exampleFun
        cleanupObj = onCleanup(@()myCleanupFun);
        for n=1:5
            pause(1)
            disp(".")
        end
        cleanupObj.cancel
        disp("Function completed successfully")
    end
    
    function myCleanupFun
        disp("Function was canceled prematurely")
    end

    运行 exampleFun

    exampleFun
    .
    .
    .
    .
    .
    Function completed successfully

    再次运行 exampleFun,这次在函数运行时使用 Ctrl+CCtrl+Break 取消执行。在 Apple Macintosh 平台上,还可以使用 Command+.(Command 键和句点键)。

    exampleFun
    .
    .
    .
    
    Function was canceled prematurely
    Operation terminated by user during exampleFun (line 4)

    输入参数

    全部折叠

    清理对象,指定为 onCleanup 对象。

    版本历史记录

    在 R2025a 中推出