Highlights from
Settings dialog

5.0

5.0 | 6 ratings Rate this file 22 Downloads (last 30 days) File Size: 183 KB File ID: #26312
image thumbnail

Settings dialog

by

 

08 Jan 2010 (Updated )

Set or change arbitrary structure via a dialog

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

The function settingsdlg() is a GUI-dialog much like MATLAB's default errordlg(), questiondlg() and warndlg(), which provides a standardized way to assign specific values to a structure. This structure can then be used to insert specific settings
 into one of MATLAB's many standard algorithms, or your own. The most basic usage is as follows:
 
[ settings, button] = settingsdlg(...
     'TolX' , 1e-6,...
     'TolFun', 1e-6);
 which will produce a nice dialog box with the fields and edit boxes as you'd expect them. After pressing OK or Cancel, the structure settings will be
 
 settings =
 
       TolX: 1.0000e-006
     TolFun: 1.0000e-006
 button =
      'ok'
 
or any relevant values you have assigned. Naturally, you can add as many fields as you want; the dialog will automatically adjust its size to match your input. If you would like the user to not just insert numeric values, but select values from a
string list, use
 
 settings = settingsdlg(...
     'TolX' , 1e-6,...
     'TolFun' , 1e-6,...
     'Algorithm', {'active-set','interior-point'});
 
 which will produce the same dialog, with a popup-list added. The resulting structure in this case is:
 
 settings =
 
          TolX: 1.0000e-006
        TolFun: 1.0000e-006
     Algorithm: 'active-set'
 
 Of course, it isn't always convenient to have the text for each option in the dialog box equal to the fieldname in the resulting structure. If you want the fieldname to be different from the displayed string, you can use something like:
 
 settings = settingsdlg(...
     {'Tolerance X' ;'TolX' }, 1e-6,...
     {'Tolerance Fun';'TolFun'}, 1e-6,...
     'Algorithm', {'active-set','interior-point'});
 
 which produces the dialog displaying the *first* entries in the cell-arrays, but the associated structure has the fieldnames
 
 settings =
 
          TolX: 1.0000e-006
        TolFun: 1.0000e-006
     Algorithm: 'active-set'
 
 Also, you can add separators, a different dialog title, and a brief description:
 
 settings = settingsdlg(...
     'Description', 'This dialog will set the parameters used by FMINCON()',...
     'title' , 'FMINCON() options',...
     'separator' , 'Unconstrained/General',...
     {'Tolerance X' ;'TolX' }, 1e-6,...
     {'Tolerance on Function';'TolFun'}, 1e-6,...
     'Algorithm' , {'active-set','interior-point'},...
     'separator' , 'Constrained',...
     {'Tolerance on Constraint';'TolCon'}, 1e-6);
 
 The 'title' and 'description' options can appear anywhere in the argument list, they will not affect the fields in the output structure. The order of the 'separator' option of course *does* matter, but, it will *not* be added as a field to the output structure. You can also use logicals, which produce checkboxes:
 
 settings = settingsdlg(...
     'Description', 'This dialog will set the parameters used by FMINCON()',...
     'title' , 'FMINCON() options',...
     'separator' , 'Unconstrained/General',...
     {'Tolerance X';'TolX'}, 1e-6,...
     {'Tolerance on Function';'TolFun'}, 1e-6,...
     'Algorithm' , {'active-set','interior-point'},...
     'separator' , 'Constrained',...
     {'This is a checkbox'; 'Check'}, true,...
     {'Tolerance on Constraints';'TolCon'}, 1e-6);
 
 which results in
 
 settings =
 
          TolX: 1.0000e-006
        TolFun: 1.0000e-006
     Algorithm: 'active-set'
         Check: 1
        TolCon: 1.0000e-006
 
 You can also assign multiple (logical!) values to a single checkbox, in which case the fields below the checkbox are all disabled/enabled when you check it:
 
 settings = settingsdlg(...
     'Description', 'This dialog will set the parameters used by FMINCON()',...
     'title' , 'FMINCON() options',...
     'separator' , 'Unconstrained/General',...
     {'This is a checkbox'; 'Check'}, [true, true],...
     {'Tolerance X';'TolX'}, 1e-6,...
     {'Tolerance on Function';'TolFun'}, 1e-6,...
     'Algorithm' , {'active-set','interior-point'},...
     'separator' , 'Constrained',...
     {'Tolerance on Constraints';'TolCon'}, 1e-6);
 
 Setting the checkbox value to [true, true] will cause the dialog box to appear with all fields below the appropriate separator disabled, whereas a value of [true, false] will have all fields initially enabled. Checking or un-checking the checkbox will simply swap the enabled/disabled states.
 
Finally, you can insert a single structure as a (single!) argument, which produces a dialog box according to its settings and fieldnames:
 
 settings = struct(...
     'TolX' , 1e-6,...
     'TolFun', 1e-6);
 settings = settingsdlg(settings);
 
 Naturally, since all other information is absent in this last example, the functionality in this case is rather limited. But if the intent is to change a fairly simple structure, it certainly suffices.

MATLAB release MATLAB 7.9 (R2009b)
Other requirements none that I know of..
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
25 Apr 2013 Matt J  
19 Apr 2013 Matt J

Very nice, and quite useful, of course. The only design choice that I find a little unfortunate is that the formatting of the uicontrols (as pop-up menu, checkbox, etc...) is selected based on the data type of the initial field values. The consequence of this is that I can really only enter doubles and strings as field data.

Sometimes, however, I want the actual value of a field to be cell-valued, e.g., {'a','b','c'}. In your set-up, though, a cell array of strings always gets interpreted as a list of pop-menu options. The same goes with fields that are logical vectors. Sometimes I want a logical vector to be interpreted as the actual data and not to be as format specifiers.

I wonder if you could have had an input syntax like the following

settingsdlg(Scontent, Sformat)

where Scontent is a structure containing the desired field values and S.format is a structure with the same field names, but specifying whether to format as checkbox, pop-up menu, etc...

15 Mar 2011 Terrance Nearey

Well done.
I've also added an optional <argumentName>,<argumentValue> pair
'UpperLeftPositionXY', [xmin,ymax]
to change position of upper left corner of dialog box on my screen. With thetwo-monitor systems I often work with, centering just doesn't work very well.

28 May 2010 Holger Hiebel

This file is great, saves lots of work.
But in order to test the buttons result more easily, I replaced all occurrences of the variable 'button' with the new variable name 'canceled' and modified some code lines:

line 93: <button = [];> replaced by <canceled=true;>
line 387: <button = 'OK';> replaced by <canceled=false;>
line 424: <button = 'cancel';> replaced by <canceled = true;>

Now I can call the dialogue ([settings,canceled]=settingsdlg(...)) and then just test with a simple if ~canceled, code, end instead using strcmp for that.
THANKS FOR THIS FILE!

18 May 2010 Rody Oldenhuis

Done!

18 May 2010 Peter

Hi Rody

thank you for your update. As I said, I use settingsdlg often.

I just found something other that could be useful and easy to implement: You set the values total_width = 250; and control_width = 100;. It would be nice if the user could set them with parameter values.

With best regards Peter

13 Jan 2010 Rody Oldenhuis

Thanks for the comment, Peter.

I just uploaded an update that implements your suggestion. SETTINGSDLG() now gives a second output argument [button]. This argument will be equal to 'ok' if you press the OK button, 'cancel' if you press the cancel button, or [] (empty) if you close the window prematurely.

13 Jan 2010 Peter

Hi Rody

thank you for this contribution. I think it is most useful. An easy and quick way to catch inputs.

I use it by displaying my default values for the user to change. I would prefer if I could catch from the function whether the user has pressed cancel and did not change anything. I could check that by comparing the result of the function but it would be easier if I could just tell he pressed cancel.

Updates
13 Jan 2010

Updated the documentation in the M-file, and added the 'button' feature, as suggested by Peter.

18 May 2010

- changed button='OK' to button='ok' to make the m-file and documentation agree
- Added "WindowWidth" and "ControlWidth", as suggested by Peter

04 Nov 2010

- Made separators boldface;
- Checkboxes and separators now span the whole width of the window (instead of ControlWidth).

14 Feb 2014

- Implemented window positioning option as suggested by Terrance Nearey below
- Begun implementation of treatment of cell-data (Matt J below)
- Updated doc & contact info

11 Jun 2014

[linked to Github]

Contact us