Matlab 2014b: graphics object deletion

1 view (last 30 days)
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 Comment
Matt J
Matt J on 21 Nov 2014
Edited: Matt J on 21 Nov 2014
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)?

Sign in to comment.

Accepted Answer

Rich Ohman
Rich Ohman on 21 Nov 2014
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 Comments
Rich Ohman
Rich Ohman on 21 Nov 2014
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 on 21 Nov 2014
Edited: Matt J on 21 Nov 2014
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?

Sign in to comment.

More Answers (1)

Matt J
Matt J on 22 Nov 2014
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 Comment
Doug Hull
Doug Hull on 24 Nov 2014
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.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!