Matlab - A Simple but Powerful Command: questdlg
14 Mar 2008 Quan Quach 10 comments 1,629 views
Introduction
Sometimes, the simplest of things can prove to be quite useful. I want to promote the questdlg command within Matlab because I think its a very useful command that people either 1) overlook, or 2) does not know that it even exists! With this command, you can create a pop-up window that will ask you a question, and will allow you to choose one of the options. For instance, if we use the following code, we can create a window that queries the user for their favorite color. Go ahead and copy and paste the following code into the Matlab command prompt.
%this example comes directly from the Matlab help ButtonName = questdlg('What is your favorite color?', ... 'Color Question', ... 'Red', 'Green', 'Blue', 'Green');

Once you click on any of the buttons, it will return the appropriate argument into the variable ButtonName. Read the rest of this tutorial for some practical examples on how to use the questdlg command.
Close GUI Confirmation
I already covered a very good use of questdlg in a previous tutorial: Close confirmation for GUI. Using the questdlg command, you can confirm if the user really wants to exit the GUI.

Save Data Confirmation
Along the same lines, you can program a GUI to ask for confirmation when the user is performing a particular action. For instance, maybe the user did not save their data and tries to perform another operation that would wipe out the current data.
ButtonName = questdlg('Do you want to save current data first?', ... 'Save Data', ... 'Yes', 'No', 'Yes'); %perform the following operation depending on the option chosen switch ButtonName, case 'Yes', %add code here for saving data case 'No', %add code here when data is not saved end % switch

Warn User of Excessive Processing Time
Perhaps the user is trying to process a data file that is very large and will take a long time. We can warn the user with the following use of questdlg, allowing them to either continuing with the data processing, or to abort it.
ButtonName = questdlg('Processing this file may take up to 30 minutes.', ... 'Process Data', ... 'Continue', 'Abort', 'Continue'); %perform the following operation depending on the option chosen switch ButtonName, case 'Continue', %continue the current operation case 'Abort', %abort the current operation end % switch

Versatile and Flexible
The questdlg command is very flexible and versatile and can be used in a variety of situations. There are many situations wherein this command is useful which I did not discuss in this tutorial. After reading this tutorial, I hope you are able to apply this knowledge and use it to create better interfaces!
10 Responses to “Matlab - A Simple but Powerful Command: questdlg”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


[...] about questdlg, a very cool little command in Matlab that I plan on making great use of. HT: blinkdagger . . . if we use the following code, we can create a window that queries the user for their [...]
I’m currently using your tutorials to complete the GUI for a thesis project. I have already learned a lot just by browsing your site, so much so that you’ll be cited in my bibliography.
Thanks for all the help,
-Mike
Wow, thanks Mike!! Hope your thesis turns out well!
Dear Quan,
This is absolutely amazing.
Thanks a lot for this very helpful post.
But i need ur help urgently in the ‘ResizeFcn’ issue i pointed out in an earlier comment.
Please do help me with the resizing of matlab GUI window.
Expecting a response at the earliest.
Regards,
Sharina
Hi Sharina,
Doug Hull over at mathworks covered that issue in this post:
http://blogs.mathworks.com/pick/2008/02/15/gui-layout-part-1/
Quan
Hi Quan,
Thanks for all the great tutorials.
Would there be a way to use the questdlg, and also record the person’s response, as well as the TIME it takes for him/her to choose that response using MATLAB?
Also, would it be possible to make it so that we can provide feedback on the person’s response. For instance, if they click Button A, they receive an ‘Incorrect’ message and try again, but if they click Button B they receive a correct message, and then another pop-up window will appear and the process starts over again??
am having problem trying to get time responses.
Response of temperature and gas generator speed to a unit step change in gas generator speed demand
dsepbt ptormsea askb mretpfua mwzsfgv oayr gzlnqayu
Good site. Thank you.
[...] cool. check this blog or the matlab help for more info. This entry was posted on Friday, May 16th, 2008 at 17:15 and is [...]