Deleted passed UIFigure handle on bootup of MATLABWindow
조회 수: 6 (최근 30일)
이전 댓글 표시
When running the following script, on the first bootup of MATLABWindow for the uifigure, sometimes the figure handle will be deleted before it can be used: the script will show "handle to deleted Figure". Moving the mouse over the window activates the error.
It doesn't happen everytime. I have found the most reliable way to produce this result is to run the script, quit out of MATLABWindow, clear the variables, and run it again.
What is going on? Is this a bug? How could I avoid it? (I want to preserve the callbacks, something about the persistence, and the one-time initialization of S. This script is a very pared down version of a more complete tool.)
clear S
S.fig= uifigure;
set(S.fig,'WindowButtonMotionFcn',{@Callback, S})
function Callback(~,~, s)
persistent S
if isempty(S)
S= s;
end
S.fig
end
댓글 수: 1
Jan
2022년 4월 18일
What does this mean: " on the first bootup of MATLABWindow for the uifigure"?
Please post the complete error message, which shows, in which line the problem occurs.
채택된 답변
Jan
2022년 4월 18일
If you run this script twice, the persistent variable S contains the handle of the former figure.
Workaround:
clear S
S.fig = uifigure;
set(S.fig,'WindowButtonMotionFcn', {@Callback, S})
function Callback(~, ~, s)
persistent S
if isempty(S) || ~isequal(S, s)
% ^^^^^^^^^^^^^^^^^ Replace formerly stored figure handle
S = s;
end
S.fig
end
Or start with clearing the callback also:
clear S
clear Callback
This resets the persistent variable.
Storing the figure handle persistently in a callback function seems to be a bad idea. The figure handle is the first input of the WindowButtonMotionFcn in every case. Performing some setup in the first call should not be hidden in this callback, but in a code block for initialising the function. If you want to know, if a figure was setup up before, use a flag in the figure's UserData or ApplicationData instead of a persistent variable in the callback. Then you get no confusion between mutliple instances of the figure.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!