MATLAB是否有退出事件? [英] Does MATLAB have an exit event?

查看:157
本文介绍了MATLAB是否有退出事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法知道MATLAB何时退出?我想做一些工作,例如释放一些资源,打印一些日志...我可以在某些类的析构函数中添加一些代码.但是由于无法确定MATLAB调用析构函数的顺序,所以我不确定哪一个是最后一个析构函数,因此可以释放资源.

Is there a way to know when an MATLAB exits? I wanted to do some work, for example, release some resource, print some logs... I could add some code at some class's destructors. But since we cannot determine the order that MATLAB calls destructors, I am not sure which one is the last one so I can release the resource.

如果存在这样的事件,我们可以向MATLAB退出事件注册任何回调吗?

Can we register any callback to the MATLAB exit event if there exists such an event...?

推荐答案

我不知道从函数或MATLAB本身退出时会抛出该退出事件.但是,您可以执行以下两项操作来进行最终清理:

There is no exit event I know of that's thrown when exiting from a function or MATLAB itself. However, there are two things you can do to handle final cleanup:

  1. 使用 onCleanUp对象:从函数,本地工作区中的变量将被破坏(并且从MATLAB本身退出将破坏基本工作区).处理资源(文件等)时,好的做法是创建一个onCleanUp对象以异常安全的方式处理那些资源. 文档和以下问题对此进行了详细讨论:如何以异常安全的方式处理MATLAB中的资源? (例如最后尝试……")

  1. Use onCleanUp objects: When exiting from a function, variables in the local workspace are destroyed (and exiting from MATLAB itself will destroy objects in the base workspace). When working with resources (files, etc.), good practice is to create an onCleanUp object to handle those resources in an exception-safe manner. This is discussed in more detail in the documentation and this question: How do you handle resources in MATLAB in an exception safe manner? (like "try … finally")

创建 finish.m 文件:何时退出MATLAB,它将查看搜索路径以查找名为finish.m的文件,并在终止之前运行该代码.

Create a finish.m file: When quitting MATLAB, it will look on the search path for a file named finish.m and run that code before terminating.

这篇关于MATLAB是否有退出事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆