Questdlg Timer
Sean's pick this week is Questdlg Timer by Az Nephi.
Contents
Background
My colleague approached me last week to ask about having a questdlg time out after a certain period of time. His use case was that he wants to ask end users about some database settings before changing them. However, if there's no person there, which there may not be if it's running in a scheduled environment or if the person is busy, after a set period of time, pick the default and keep going. Here's the question he was asking:
So what's the challenge? The questdlg is a modal dialog box meaning execution of the command line, script, or function, is held until the dialog is closed. This means that I can't programmatically close it after a pause.
My Attempt
So the first thing I did was search the File Exchange and MATLAB answers. The File Exchange didn't turn up anything promising (searching for "timeout") and MATLAB answers had a few questions where this was asked.
Obviously, I could build the whole thing from scratch, but that seems like a lot of overhead. Instead, I'll use a timer which emulates a second thread and fires periodically after an optional delay.
- The 'StartDelay' will be the timeout.
- The 'TimerFcn' will close the modal figure.
- By default, timers only fire once.
We'll build and start the timer, then build the questdlg. Additionally, tic and toc are around the question dialog to see that it times out.
t = timer('StartDelay',10,... 'TimerFcn',@(~,~)delete(findall(groot,'WindowStyle','modal'))); start(t) tic answer = questdlg('Do you want to use the ODBC driver?','Driver','ODBC','JDBC','Cancel','ODBC')
answer = 0×0 empty char array
toc
Elapsed time is 10.037885 seconds.
I'd now have to test if answer is empty and define it to be the default if it is. Challenge complete!
Searching
So then I was going to polish this idea and push it to the File Exchange but figured I'd search a little more first. This time, I searched for "questdlg".
Face palm!
Thanks Az, you wrote exactly what I needed! And it is much simpler, Az just modifed the call to uiwait to use the built in 'timeout' option.
answer = questdlg_timer(10,'Do you want to use the ODBC driver?','Driver','ODBC','JDBC','Cancel','ODBC')
answer = 1×4 char array ODBC
Comments
Give it a try and let us know what you think here or leave a comment for Az.
- カテゴリ:
- Picks
コメント
コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。