Matlab - A Simple but Powerful Command: questdlg
14 Mar 2008 Quan Quach 15 comments 2,840 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!
15 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 [...]
Hi guys,
Great site, sooo useful!!
Im wondering if a questdlg can have more than 3 buttons.
I need to have 4 and matlab is telling me i have too many input arguments.
Thanks
Astromoof,
Unfortunately, from what I’ve seen, 3 is the maximum number of options. I did find a possible alternative though… but it’s not as pretty.
Make a string out of your options Then have the user choose from the list.
It is a little trickier if you want to select a couple options, but that doesn’t sound like you need to do that if you were just gonna use questdlg anyways.
Requires one extra click more than questdlg would (select, then ‘ok’) but it should do the job.
Good luck!
-Zane
Hi Zane,
It’s excellent! Revolutionary for my code, thank you soooo much.
Im trying to idiot proof my code by getting users to pick things instead of type inputs (without me having to write a full-on GUI)
Only problem now is that i can implement this listdlg in so many places now, got alot of work ahead
Thank you so much, i love your site
Hey Guys, Im going nuts.
Im trying to write a function that pulls the names of materials in from a xls file. The way ive written it is so that if someone changes one of the materials in the xls file, the listdlg will have that name in it too.
my problem is that is seems all the material names have to be the same lenght or something.
Can i send you my xls file to run my come below to see if you get the “matrix dimensions must agree” error.
Thanks from Astromoof
the xls is basically (the names start in row 4)
(in column 2) (in column 3) (in column 4) (in column 5)
MetalA 1.0 2.0 3.0
MetalB 2.0 6.0 5.0
MetalC (3 values for each material is filled in)
MetalD
Carbon_1
Cardon_2
Plactic_1
Plactic_2
Additional1
Additional2
Additional3
Additional4
Additional5
Additional6
Additional7
Additional8
Additional9
Additional10
Additional11
Additional12
Hi Guys,
Im such an idiot, i figured out a way to do it.
I appreciate that you would have helped me (if i didnt ask stupid questions)
THanks