{"id":7749,"date":"2016-08-05T09:00:55","date_gmt":"2016-08-05T13:00:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=7749"},"modified":"2017-08-27T20:05:00","modified_gmt":"2017-08-28T00:05:00","slug":"questdlg-timer","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/08\/05\/questdlg-timer\/","title":{"rendered":"Questdlg Timer"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/32977-questdlg-timer\">Questdlg Timer<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2583766\">Az Nephi<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Background<\/a><\/li>\r\n         <li><a href=\"#2\">My Attempt<\/a><\/li>\r\n         <li><a href=\"#5\">Searching<\/a><\/li>\r\n         <li><a href=\"#7\">Comments<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Background<a name=\"1\"><\/a><\/h3>\r\n   <p>My colleague approached me last week to ask about having a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/questdlg.html\"><tt>questdlg<\/tt><\/a> time out after a certain period of time.  His use case was that he wants to ask end users about some <a href=\"https:\/\/www.mathworks.com\/products\/database\/\">database<\/a> settings before changing\r\n      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\r\n      is busy, after a set period of time, pick the default and keep going.  Here's the question he was asking:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainquestdlg\/questdlg.png\"> <\/p>\r\n   <p>So what's the challenge?  The <tt>questdlg<\/tt> is a modal dialog box meaning execution of the command line, script, or function, is held until the dialog is closed.  This means that\r\n      I can't programmatically close it after a <tt>pause<\/tt>.\r\n   <\/p>\r\n   <h3>My Attempt<a name=\"2\"><\/a><\/h3>\r\n   <p>So the first thing I did was search the File Exchange and MATLAB answers. The File Exchange didn't turn up anything promising\r\n      (searching for \"timeout\") and MATLAB answers had a few questions where this was asked.\r\n   <\/p>\r\n   <p>Obviously, I could build the whole thing from scratch, but that seems like a lot of overhead.  Instead, I'll use a <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/timer-class.html (link no longer works)\"><tt>timer<\/tt><\/a> which emulates a second thread and fires periodically after an optional delay.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>The <i>'StartDelay'<\/i> will be the timeout.\r\n         <\/li>\r\n         <li>The <i>'TimerFcn'<\/i> will close the modal figure.\r\n         <\/li>\r\n         <li>By default, timers only fire once.<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>We'll build and start the timer, then build the <tt>questdlg<\/tt>. Additionally, tic and toc are around the question dialog to see that it times out.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">t = timer(<span style=\"color: #A020F0\">'StartDelay'<\/span>,10,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'TimerFcn'<\/span>,@(~,~)delete(findall(groot,<span style=\"color: #A020F0\">'WindowStyle'<\/span>,<span style=\"color: #A020F0\">'modal'<\/span>)));\r\nstart(t)\r\n\r\ntic\r\nanswer = questdlg(<span style=\"color: #A020F0\">'Do you want to use the ODBC driver?'<\/span>,<span style=\"color: #A020F0\">'Driver'<\/span>,<span style=\"color: #A020F0\">'ODBC'<\/span>,<span style=\"color: #A020F0\">'JDBC'<\/span>,<span style=\"color: #A020F0\">'Cancel'<\/span>,<span style=\"color: #A020F0\">'ODBC'<\/span>)<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainquestdlg\/questdlg.png\"> <\/p><pre style=\"font-style:oblique\">answer =\r\n  0&times;0 empty char array\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">toc<\/pre><pre style=\"font-style:oblique\">Elapsed time is 10.037885 seconds.\r\n<\/pre><p>I'd now have to test if <i>answer<\/i> is empty and define it to be the default if it is.  Challenge complete!\r\n   <\/p>\r\n   <h3>Searching<a name=\"5\"><\/a><\/h3>\r\n   <p>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\r\n      time, I searched for \"questdlg\".\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainquestdlg\/questdlgtimer.png\"> <\/p>\r\n   <p>Face palm!<\/p>\r\n   <p>Thanks Az, you wrote exactly what I needed!  And it is much simpler, Az just modifed the call to <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/uiwait.html\"><tt>uiwait<\/tt><\/a> to use the built in <i>'timeout'<\/i> option.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">answer = questdlg_timer(10,<span style=\"color: #A020F0\">'Do you want to use the ODBC driver?'<\/span>,<span style=\"color: #A020F0\">'Driver'<\/span>,<span style=\"color: #A020F0\">'ODBC'<\/span>,<span style=\"color: #A020F0\">'JDBC'<\/span>,<span style=\"color: #A020F0\">'Cancel'<\/span>,<span style=\"color: #A020F0\">'ODBC'<\/span>)<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainquestdlg\/questdlgtimer2.png\"> <\/p><pre style=\"font-style:oblique\">answer =\r\n  1&times;4 char array\r\nODBC\r\n<\/pre>\r\n   <h3>Comments<a name=\"7\"><\/a><\/h3>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=7749#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/32977-questdlg-timer#comments\">comment<\/a> for Az.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_7920fbeaf04340b1ae1032c42fca5f0b() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='7920fbeaf04340b1ae1032c42fca5f0b ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 7920fbeaf04340b1ae1032c42fca5f0b';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Sean de Wolski';\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_7920fbeaf04340b1ae1032c42fca5f0b()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016b<br><\/p>\r\n<\/div>\r\n<!--\r\n7920fbeaf04340b1ae1032c42fca5f0b ##### SOURCE BEGIN #####\r\n%% Question Dialog with Timeout\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/32977-questdlg-timer Questdlg Timer> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2583766 Az Nephi>.\r\n% \r\n\r\n%% Background\r\n% My colleague approached me last week to ask about having a\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/questdlg.html |questdlg|> time\r\n% out after a certain period of time.  His use case was that he wants to\r\n% ask end users about some settings before changing them.  However, if\r\n% there's no person there, which there may not be if it's running in a\r\n% scheduled environment or if the person is busy, after a set period of\r\n% time, pick the default and keep going.  Here's the question he was asking:\r\n%\r\n% <<questdlg.png>>\r\n%\r\n% So what's the challenge?  The |questdlg| is a\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/figure-properties.html modal>\r\n% dialog box meaning execution of the command line, script, or function, is\r\n% held until the dialog is closed.  This means that I can't\r\n% programmatically close it after a |pause|.\r\n\r\n%% My Attempt\r\n% So the first thing I did was search the File Exchange and MATLAB answers.\r\n% The File Exchange didn't turn up anything promising (searching for\r\n% \"timeout\") and MATLAB answers had a few questions where this was asked.\r\n%\r\n% Obviously, I could build the whole thing from scratch, but that seems\r\n% like a lot of overhead.  Instead, I'll use a\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/timer-class.html |timer|> which\r\n% emulates a second thread and fires periodically after an optional delay.\r\n% \r\n% * The _'StartDelay'_ will be the timeout.\r\n% * The _'TimerFcn'_ will close the modal figure.\r\n% * By default, timers only fire once.\r\n%\r\n% We'll build and start the timer, then build the |questdlg|. Additionally,\r\n% tic and toc are around the question dialog to see that it times out.\r\n\r\nt = timer('StartDelay',10,...\r\n    'TimerFcn',@(~,~)delete(findall(groot,'WindowStyle','modal')));\r\nstart(t)\r\n\r\ntic\r\nanswer = questdlg('Do you want to use the ODBC driver?','Driver','ODBC','JDBC','Cancel','ODBC')\r\n%%\r\n%\r\n% <<questdlg.png>>\r\ntoc\r\n\r\n%%\r\n% I'd now have to test if _answer_ is empty and define it to be the default\r\n% if it is.  Challenge complete!\r\n\r\n\r\n%% Searching\r\n% So then I was going to polish this idea and push it to the File Exchange\r\n% but figured I'd search a little more first.  This time, I searched for\r\n% \"questdlg\".\r\n%\r\n% <<questdlgtimer.png>>\r\n%\r\n% Face palm!  \r\n%\r\n% Thanks Az, you wrote exactly what I needed!\r\n\r\nanswer = questdlg_timer(10,'Do you want to use the ODBC driver?','Driver','ODBC','JDBC','Cancel','ODBC')\r\n\r\n%%\r\n%\r\n% <<questdlgtimer2.png>>\r\n\r\n%% Comments\r\n% \r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=7749#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/32977-questdlg-timer#comments\r\n% comment> for Az.\r\n%\r\n \r\n\r\n##### SOURCE END ##### 7920fbeaf04340b1ae1032c42fca5f0b\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainquestdlg\/questdlg.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is Questdlg Timer by Az Nephi.\r\n      \r\n   \r\n   Contents\r\n   \r\n      \r\n         Background\r\n... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/08\/05\/questdlg-timer\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7749"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=7749"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7749\/revisions"}],"predecessor-version":[{"id":8837,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7749\/revisions\/8837"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=7749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=7749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=7749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}