I have a function in my app that I use to pause the program and show an alert. Most of the time it works without issue.
function PauseAlert(app, msg, title, icon)
if nargin < 4
icon = 'error';
end
beep;
uialert(app.UIFigure, msg, title,'CloseFcn','uiresume(gcbf)','Icon',icon);
drawnow;
figure(app.UIFigure);
uiwait(gcbf);
end
However, if it try to use it from within the startupFcn, the program halts on the last line with the following error message:
Error using uiwait (line 48)
Input argument must be of type figure
And it gives another error when I close the uialert message:
Warning: Error while evaluating 'CloseFcn' for dialog.
Error using uiresume (line 26)
Argument must be a Figure object.
I could probably do something like the following:
if ishghandle(h)
uiwait(gcbf)
end
but then the program wouldn't pause like I want it to.

 채택된 답변

Cris LaPierre
Cris LaPierre 2023년 6월 12일

1 개 추천

It sounds like the figure gcbf does not yet exist when you try to call uiwait.

댓글 수: 7

James Moore
James Moore 2023년 6월 12일
편집: James Moore 2023년 6월 12일
That was sort of my suspicion as well. At the time when uiwait is called, gcbf returns an empty matrix. It also seems like the function is called before the app completely is completely loaded. I guess my question then is why wasn't the figure created yet, and how do I delay the function from executing until it is ready? I tried the following, thinking I could maybe delay it until the app window finishes loading, but gcbf never gets created, and it never leaves the while loop even after the app window appears.
while (isempty(gcbf))
pause(1);
end
uiwait(gcbf);
I found a workaround, although it isn't very pretty. If gcbf is empty, I just create a useless figure window for the sole purpose of closing it to resume the program after the alert is closed.
function PauseAlert(app, msg, title, icon)
if nargin < 4
icon = 'error';
end
beep;
if isempty(gcbf)
f=figure; %Makes a dummy figure if gcbf has not been created.
uialert(app.UIFigure, msg, title,'CloseFcn',@(h,e) close(f),'Icon',icon);
drawnow;
figure(app.UIFigure);%Return main window to front
uiwait(f);
else
uialert(app.UIFigure, msg, title,'CloseFcn','uiresume(gcbf)','Icon',icon);
uiwait(gcbf);
end
drawnow;
figure(app.UIFigure);
end
Cris LaPierre
Cris LaPierre 2023년 6월 12일
편집: Cris LaPierre 2023년 6월 12일
Just speculating here, but gcbf points to the figure whose callback is executing. When called from the startupFcn, if returns [], indicating that no callback is executing (see here).
The description of the startupFcn is "App Designer allows you to create a special function that executes when the app starts up, but before the user interacts with the UI".
Callbacks are a result of interacting with the UI. You many therefore need to adjust the code when called from your startupFcn to handle the 'no callback' case.
I won't say I love this code, but it appears to work.
function PauseAlert(app, msg, title, icon)
if nargin < 4
icon = 'error';
end
tmp = gcbo;
if isempty(tmp)
fig = app.UIFigure;
else
fig = gcbf;
end
assignin("base","fig",fig)
beep;
uialert(app.UIFigure, msg, title,'CloseFcn','uiresume(fig)','Icon',icon);
drawnow;
figure(app.UIFigure);
uiwait(fig);
end
James Moore
James Moore 2023년 6월 13일
I tried the code, but I'm still getting the same error as before. Stepping through it in debug mode shows that fig is still an empty matrix at the time of execution.
It might be better to put this part of the code in a callback function that runs first thing after the UI functionality of the app is fully initialized if such a thing exists. I couldn't find anything else in the MATLAB documentation though.
For some context, during the startupFcn my application reads from several data files and I want to show an alert to the user if one or more files are missing, then bring up a browser window to let the user try to find the file. Bringing up the browser window does pause the program's execution, so I could just include the message in the title of the browser window, but I'm not sure this will be clear to the user, so I want to make sure the program pauses after bringing up the alert.
Cris LaPierre
Cris LaPierre 2023년 6월 13일
편집: Cris LaPierre 2023년 6월 13일
It works in R2023a, but not R2020a. You can fix that my modifying the code I shared to the following:
function PauseAlert(app, msg, title, icon)
if nargin < 4
icon = 'error';
end
beep;
fig = gcbf;
if isempty(fig)
fig = app.UIFigure;
end
assignin("base","fig",fig)
uialert(app.UIFigure, msg, title,'CloseFcn','uiresume(fig)','Icon',icon);
drawnow;
figure(app.UIFigure);
uiwait(fig);
end
Just a note that this code also works in R2023a as well. For whatever reason, when I was testing it yesterday, it did not.
James Moore
James Moore 2023년 6월 13일
Yes this works for me as well. Thanks for your help, I'll mark this answer as accepted.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2023년 6월 12일

편집:

2023년 6월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by