Introduction

Matlab Logo 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');

Favorite Color

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.

Close Gui Confirmation

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

Save Data

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

Large Data Set

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!