Matlab 2014b: graphics object deletion

조회 수: 3 (최근 30일)
Matt J
Matt J 2014년 11월 21일
댓글: Doug Hull 2014년 11월 24일
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