File Exchange Pick of the Week

Our best user submissions

Questdlg Timer

Sean's pick this week isQuestdlg TimerbyAz Nephi.

Contents

Background

My colleague approached me last week to ask about having aquestdlgtime out after a certain period of time. His use case was that he wants to ask end users about somedatabasesettings 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:

所以what's the challenge? Thequestdlgis 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 apause.

My Attempt

所以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 atimerwhich emulates a second thread and fires periodically after an optional delay.

  • The'StartDelay'will be the timeout.
  • The'TimerFcn'模态figu将关闭re.
  • By default, timers only fire once.

We'll build and start the timer, then build thequestdlg. 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 ifansweris empty and define it to be the default if it is. Challenge complete!

Searching

所以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 touiwaitto 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 thinkhereor leave acommentfor Az.




Published with MATLAB® R2016b

|
  • print
  • send email

Comments

To leave a comment, please clickhereto sign in to your MathWorks Account or create a new one.