Matlab 2014b: graphics object deletion

3 visualizaciones (últimos 30 días)
Matt J
Matt J el 21 de Nov. de 2014
Comentada: Doug Hull el 24 de Nov. de 2014
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 comentario
Matt J
Matt J el 21 de Nov. de 2014
Editada: Matt J el 21 de Nov. de 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)?

Iniciar sesión para comentar.

Respuesta aceptada

Rich Ohman
Rich Ohman el 21 de Nov. de 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 comentarios
Rich Ohman
Rich Ohman el 21 de Nov. de 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 el 21 de Nov. de 2014
Editada: Matt J el 21 de Nov. de 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?

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 22 de Nov. de 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 comentario
Doug Hull
Doug Hull el 24 de Nov. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by