How can I avoid errors related to a datatip when deleting a figure?

4 次查看(过去 30 天)
I have a figure that contains an axes with a plot on it. When the figure is closed, I save the plot by setting the axes parant to [] and then deleting the figure.
If a user has clicked on the line to create datatips, the when I delete the figure, I get errors in MATLAB that seem to be related to the datatip. Based on the error messages, I believe these are related to the context menu of the datatip. Is there a way to avoid getting these errors?
I have tried to set the context menu of the data tip, but it seems that property is not settable:
Warning: You cannot set 'ContextMenu' property of DataTip.
I have included a very short sample that demonstrates this:
%% Create a simple plot
f = figure;
p = uipanel(f,'Units','normalized','Position',[0 0 1 1]);
ax = axes(p);
plot(ax,1:10);
%% Interactively create a data tip
%% Delete the figure, but not the panel
p.Parent = [];
delete(f);
This generates the following error:
Warning: Error executing listener callback for PostSet event on UIContextMenu dynamic property in object of matlab.graphics.datatip.DataTip class:
Undefined function 'isvalid' for input arguments of type 'double'.
Error in matlab.graphics.datatip.DataTip/showWarningOnSetUIContextMenu
Error in matlab.graphics.datatip.DataTip>@(obj,evd)hObj.showWarningOnSetUIContextMenu()
Error in matlab.graphics.shape.internal.DataCursorManager/delete
Error in matlab.graphics.shape.internal.DataCursorManager>@(obj,evd)(delete(hObj))
Error in matlab.uitools.internal.uimode/createuimode>localDelete (line 137)
delete(hThis);
Error in matlab.uitools.internal.uimode/createuimode>@(obj,evd)(localDelete(hThis)) (line 18)
hThis.FigureDeleteListener = matlab.ui.internal.createListener(hFig,'ObjectBeingDestroyed',@(obj,evd)(localDelete(hThis)));
> In matlab.graphics.shape.internal/DataCursorManager/delete
In matlab.graphics.shape.internal.DataCursorManager>@(obj,evd)(delete(hObj))
In matlab.uitools.internal.uimode/createuimode>localDelete (line 137)
In matlab.uitools.internal.uimode/createuimode>@(obj,evd)(localDelete(hThis)) (line 18)

回答(1 个)

Sean de Wolski
Sean de Wolski 2022-5-20
Not sure why you're parenting to [], that doesn't make sense. Just delete the figure with your MWE works fine.
(Still probably a bug that that warning appears)
  1 个评论
Matt Butts
Matt Butts 2022-5-20
I am unparenting the panel because I will be using it again (drawing it the first time was quite intensive). I delete the panel later based other user actions.
However, now that you asked this question, maybe just turning the visibility off would work better?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by