How to delete a graphics object when clearing the object handle variable?

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 件のコメント

f = figure;
delete(f)
clear f;
Jai Bhagat
Jai Bhagat 2019 年 4 月 8 日
編集済み: Jai Bhagat 2019 年 4 月 8 日
I guess I needed to clarify - I want clearing the figure handle to also delete the object. I don't want to run a separate line of code:
delete(f)
to do so.
Essentially, I want a listener for the
clear f
command that also deletes the object, 'f', when the handle is cleared
dpb
dpb 2019 年 4 月 8 日
Sometimes we just can't have what we want.
What's the big deal about having a second line of code?
Guillaume
Guillaume 2019 年 4 月 8 日
You seem to be mistaking a variable containing a handle with the handle itself.
What should happen in this case:
f = figure;
g = f; %a variable containing the same handle
clear f %should the figure close or not?
In general, you should never call clear anyway. Using it is usually an indication of bad programming practice.
Jai Bhagat
Jai Bhagat 2019 年 4 月 8 日
"You seem to be mistaking a variable containing a handle with the handle itself." @Guillaume
I wouldn't say I was mistaken, but yes I could have been clearer - I'd like to have it so when a variable (any variable) referencing the graphics object handle is cleared, the object is deleted.
"What's the big deal about having a second line of code?" @dbp
I'd like to have this because when instantiating an object from a class which creates a separate graphics object, I want it so that when the client clears the variable referring to the ancestor object, all the children graphics objects are deleted. In this way, a client could instantiate multiple objects from this class in the workspace, and not be confused as to which graphics objects belong to which ancestor objects upon clearing of the variable referring to the ancestor object.
"you should never call clear anyway" @Guillaume
Please elaborate? E.g. I find it helpful to free up RAM, I find it helpful to clear references to hardware objects in order to re-use those same hardware objects again in the same MATLAB session when doing exploratory testing, etc...
Guillaume
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
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
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 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2019 年 4 月 8 日

コメント済み:

2019 年 4 月 8 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by