Dealing with deleted hggroups

1 回表示 (過去 30 日間)
HILTON MARQUES SANTANA
HILTON MARQUES SANTANA 2022 年 7 月 21 日
回答済み: Abhimenyu 2023 年 10 月 19 日
Hello, I am building a simple GUI program and dealing with Mouse Move on this way:
function onMouseMove(this,~,~)
if ~isempty(this.m_hnd)
delete(this.m_hnd)
this.m_hnd = gobjects(0);
end
for i=1:app.PillarsStatsPanel.Children.GridSize(1)
this.m_hnd(i) = hggroup(app.PillarsStatsPanel.Children.Children(i));
h1 = text(app.PillarsStatsPanel.Children.Children(i),this.GetX(),this.GetY(),this.GetText(),'Parent',this.m_hnd(i));
...
end
end
But infrequently, this error appears:
Error using text
Cannot set property to a deleted object.
Error in ChartXYToolbar/onMouseMove (line 204)
h1 = text(app.PillarsStatsPanel.Children.Children(i),this.GetX(),this.GetY(),this.GetText(),'Parent',this.m_hnd(i));
Error in ChartXYToolbar>@(varargin)this.onMouseMove(varargin{:}) (line 47)
set(app.BAICAUIFigure,'windowbuttonmotionfcn',@this.onMouseMove);
Error while evaluating Figure WindowButtonMotionFcn.
I have no idea what is causing this error, any thoughts?

回答 (1 件)

Abhimenyu
Abhimenyu 2023 年 10 月 19 日
Hi Hilton,
I understand that the 'onMouseMove callback function of your GUI program is throwing an error. The error is related to text object because it is being deleted by some other process before its properties can be set. Please follow the given recommendations to debug your code:
  1. Check if the `PillarsStatsPanel` or its children are being modified or deleted elsewhere in your code. It is possible that another part of your program is modifying the GUI elements while the `onMouseMove` function is still running.
  2. Verify that the `m_hnd` array is correctly initialized and updated. Make sure that the number of elements in `m_hnd` matches the number of children in `PillarsStatsPanel`. If the number of children changes dynamically, you may need to update `m_hnd` accordingly.
  3. Check if the `app.PillarsStatsPanel.Children.Children(i)` object is valid and not deleted before calling the `text` function. You can add a check to ensure that the object is not empty or deleted. Here is an example code using ‘try-catch’ block and ‘isvalid’ function to ensure that `app.PillarsStatsPanel.Children.Children(i)` is a valid object before attempting to create the text object.
function onMouseMove(this, ~, ~)
if ~isempty(this.m_hnd)
delete(this.m_hnd);
this.m_hnd = gobjects(0);
end
for i = 1:numel(app.PillarsStatsPanel.Children.Children)
childObj = app.PillarsStatsPanel.Children.Children(i);
if ~isempty(childObj) && isvalid(childObj)
this.m_hnd(i) = hggroup(childObj);
try
h1 = text(childObj, this.GetX(), this.GetY(), this.GetText(), 'Parent', this.m_hnd(i));
% Additional code for customizing the text object if needed
% ...
catch err
%add a warning message
end
end
end
end
The above recommendations should help to troubleshoot your issues.
For more information on ‘try-catch’ block and ‘MATLAB code analysis’ for debugging, please follow the MATLAB documentation links given below:
I hope this helps!
Thank you,
Abhimenyu.

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by