How to delete a graphics object when clearing the object handle variable?
4 次查看(过去 30 天)
显示 更早的评论
E.g.
f = figure;
clear f;
How can I programtically make it so that the figure that is created will be closed when the second line of code is run?
I.e., I want clearing the graphics handle to also delete the graphics object.
7 个评论
Guillaume
2019-4-8
I find it helpful to clear references to hardware objects
delete the handle instead of clearing a variable containing the handle. This guarantees that the destructor of the object is closed. If you clear a variable, the destructor won't be called until all the other variables holding a reference to that handle are also cleared.
This is the crux of your problem. With figures, matlab keeps a handle to it around, so even if you clear all variables your figure will still hang around. Other than hacky workarounds as written by Matt, there is no way to achieve what you want.
Sure, if you're doing debugging/testing on the command line, you use clear. But in code, there shouldn't be any need for clear.
Matt J
2019-4-8
I find it helpful to free up RAM
Freeing up RAM should be its primary use. But handle objects wouldn't normally consume that much RAM, and it doesn't appear that your purpose here is to conserve RAM anyway, but rather to destroy the graphic.
采纳的回答
Matt J
2019-4-8
编辑:Matt J
2019-4-8
Here is a one-line solution, but you have to pass an actual named variable for it to work properly,
>> delclear(f) %one line
function delclear(f)
delete(f);
name=inputname(1);
if ~isempty(name)
evalin('caller', ['clear ',inputname(1)]) ;
else
warning 'Deleted, but not cleared.'
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!