Is it possible to have a uiconfirm choose an option after a timeout period?

조회 수: 7 (최근 30일)
I am trying to modify my uiconfirm popup to select the 'DefaultOption' after some time to avoid having the popup open for a long period of time and holding up code execution.
I have already tried using a timer to select one of the options which didn't seem to work but maybe I did it wrong.
I can explain what I am trying to do and why in further detail if needed.
Here is a barebones example of what I am trying to modify:
closeHdlg = uiconfirm(MainFigure, "Title",'Program','Options',{'Retry','Disconnect','Continue'},'DefaultOption','Retry');
switch closeHdlg
case 'Retry'
% Hidden for confidentiality
case 'Disconnect'
% Hidden for confidentiality
case 'Continue'
% Hidden for confidentiality
case ''
% close box and do nothing
end % end switch
  댓글 수: 2
Stephen23
Stephen23 2025년 1월 7일
편집: Stephen23 2025년 1월 7일
If you need a dialog that can be programmatically controlled (e.g. with a timer), you could use DIALOG.
Or you could just roll-your-own using a modal UIFIGURE.
Ryan
Ryan 2025년 1월 7일
I'll give that a try and I'll report back, thanks.

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

채택된 답변

Madheswaran
Madheswaran 2025년 1월 9일
You can achieve a timeout by using 'dialog' function. Consider the below code:
function showTimeoutDialogMini()
fig = uifigure('Position', [300 300 200 100]);
uibutton(fig, 'Text', 'Show Dialog', ...
'Position', [50 35 100 30], ...
'ButtonPushedFcn', @showDialog);
function showDialog(~,~)
d = dialog('Position', [350 350 250 150], ...
'Name', 'Confirmation');
uicontrol(d, 'Style', 'text', ...
'String', 'Dialog will timeout in 5 seconds', ...
'Position', [20 80 210 40]);
uicontrol(d, 'Style', 'pushbutton', ...
'String', 'Proceed', ...
'Position', [30 20 80 30], ...
'Callback', @(~,~)closeDialog('Retry'));
uicontrol(d, 'Style', 'pushbutton', ...
'String', 'Cancel', ...
'Position', [140 20 80 30], ...
'Callback', @(~,~)closeDialog('Cancel'));
t = timer('ExecutionMode', 'singleShot', ...
'StartDelay', 5, ...
'TimerFcn', @(~,~)timeoutClose());
start(t);
function closeDialog(action)
if isvalid(t), stop(t); delete(t); end
disp([action ' selected']);
delete(d);
end
function timeoutClose()
if isvalid(d)
disp('Timeout - Cancel selected');
delete(d);
end
end
end
end
This code creates a simple dialog box with two options ("Proceed" and "Cancel") and an automatic timeout feature. When you click the "Show Dialog" button, it opens a confirmation dialog that will automatically close and select "Cancel" after 5 seconds if no option is selected. The code uses MATLAB's timer object to handle the timeout functionality.
For more information refer to the following documentation: https://www.mathworks.com/help/matlab/ref/dialog.html
Hope this helps!
  댓글 수: 1
Ryan
Ryan 2025년 2월 11일
It took a bit of finagling to get this to work with my use case but it seems to be accomplishing the intended behavior, thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by