Maybe another way to put the question is, "when an axes object is deleted, is there any documented guarantee of what order subordinate things get deleted in"? Should its children (e.g., text boxes) always be deleted before its property content (e.g., UserData)?
Matlab 2014b: graphics object deletion
2 次查看(过去 30 天)
显示 更早的评论
I am comparing the behavior of the test code below in both R2013b and R2014b. In R2013b, running test.m results in the message 'User Data Present' being printed to the screen. However, when I run the very same code in R2014b, the message 'No User Data' is printed. Can anyone reproduce this and explain why the results are different?
function test
imagesc(ones(100));
options = {'The Text','HorizontalAlignment', 'center',...
'color' , 'yellow','HitTest','off',...
'DeleteFcn',@MyDelete};
text(50,50,options{:});
set(gca,'UserData','User Data Present');
close(gcf)
function MyDelete(~,~)
d=get(gca,'UserData');
if isempty(d)
disp('No User Data')
else
disp(d)
end
采纳的回答
Rich Ohman
2014-11-21
It is generally not safe to use gcf, gca, and gca in a callback function since these are both user-settable and could be modified internally if interrupted by another callback. You should use gcbo or gcbf to get the current object or current figure from within a callback function.
If you replace the MyDelete function with the following code, you will get the correct behavior:
function MyDelete(~,~)
obj = gcbo;
ax = get(obj,'Parent');
d=get(ax,'UserData');
if isempty(d)
disp('No User Data')
else
disp(d)
end
3 个评论
Rich Ohman
2014-11-21
Remember that gca creates a new axes if it cannot use the figure's CurrentAxes property value. In this case, the original axes is being destroyed, so gca created a new one. This is why the UserData was empty, the default value for a new axes.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!