打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Difference between clear, delete, and close

Object Lifecycle

An object's lifecycle ends when:

  • You reassign a new value to that variable.

  • The object is no longer used in a function.

  • Function execution ends.

MATLAB? handle classes have a special method called delete that MATLAB calls when a handle object lifecycle ends.

Calling delete on an object explicitly makes all copies of a handle object invalid because it destroys the data associated with the object and frees memory used by deleted objects. MATLAB calls delete automatically so it is not necessary for you to do so. Classes can redefine the handle class delete method to perform other cleanup operations, like closing files or saving data.

Deleting a handle object renders all copies invalid:

hobj1 = HdClass(8);hobj2 = hobj1;delete(hobj1)hobj2.DataInvalid or deleted object.

Difference Between clear and delete

The handle class delete method removes the handle object, but does not clear the variable name. The clear function removes a variable name, but does not remove the values to which the variable refers. For example, if you have two variables that refer to the same handle object, you can clear either one without affecting the actual object:

hobj = containers.Map({'Red Sox','Yankees'}, {'Boston','New York'});hobj_copy = hobj;clear hobjcity = hobj_copy('Red Sox')city =Boston

If you call clear on all handle variables that refer to the same handle object, then you have lost access to the object and MATLAB destroys the object. That is, when there are no references to an object, the object ceases to exist.

On value objects, you can call clear to remove the variable. However, MATLAB does not automatically call a value class delete method, if one exists, when you clear the variable.


Difference Between close(h) and delete(h)

As you know, close(h) and delete(h) both methods can close figurewindow, when h is a handle of a figure. What's the difference betweenthe two methods?

CLOSE calls the function specified in the CloseRequestFcn property of the figure. DELETE does not.

f1 = figure('CloseRequestFcn', 'disp(''hi'')');
close(f1)
delete(f1)

Note that the figure still exists after the second line of code (and that line of code displays the word hi in the Command Window) while it does not exist after the third line, and that the third line does not cause the word hi to be displayed in the Command Window.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Core Java FAQ’S
COM Reference
Renaud Waldura‘s The Final Word On the final Keyword
Objective-C 编程语言官网文档(三)-如何定义类
PB try 使用
ucgui_向按钮发送一个按键消息的程序追踪
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服