Matlab 2014b: graphics object deletion

3 次查看(过去 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
  1 个评论
Matt J
Matt J 2014-11-21
编辑:Matt J 2014-11-21
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)?

请先登录,再进行评论。

采纳的回答

Rich Ohman
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
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.
Matt J
Matt J 2014-11-21
编辑:Matt J 2014-11-21
But that brings me back to my original question as rephrased here
How can the original axes be destroyed until all of its children (in particular the text box in the process of executing MyDelete() ) finish self-destructing first? Are you saying object deletions aren't sequential or synchronized according to the parent-child relationship? It's just luck or undocumented behavior that in R2013b, the text box always succeeds in deleting itself before its parent axes?

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2014-11-22
I just heard back from Tech Support. They have categorized it as a bug, so I don't quite know how to reconcile that with Rich's answer. I suppose then that there is supposed to be a certain sequence to object deletion...?
  1 个评论
Doug Hull
Doug Hull 2014-11-24
There is a certain sequence to object deletion, but it is undocumented and subject to change. It should not be relied upon. Rich's assement that use of convenience functions such as GCA is unadvisable is still true.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by