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
Alex Mcaulley
2019년 4월 8일
f = figure;
delete(f)
clear f;
Jai Bhagat
2019년 4월 8일
편집: Jai Bhagat
2019년 4월 8일
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
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
2019년 4월 8일
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 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!