Navigation

Operators and Keywords

Function List:

C++ API

: [sel, ok] = listdlg (key, value, …)

Return user inputs from a list dialog box in a vector of selection indices (sel) and a flag indicating how the user closed the dialog box (ok).

The indices in sel are 1-based.

The value of ok is 1 if the user closed the box with the OK button, otherwise it is 0 and sel is empty.

Input arguments are specified in form of key, value pairs. The "ListString" argument pair must be specified.

Valid key and value pairs are:

"ListString"

a cell array of strings with the contents of the list.

"SelectionMode"

can be either "Single" or "Multiple" (default).

"ListSize"

a vector with two elements width and height defining the size of the list field in pixels. Default is [160 300].

"InitialValue"

a vector containing 1-based indices of preselected elements. Default is 1 (first item).

"Name"

a string to be used as the dialog caption. Default is "".

"PromptString"

a cell array of strings to be displayed above the list field. Default is {}.

"OKString"

a string used to label the OK button. Default is "OK".

"CancelString"

a string used to label the Cancel button. Default is "Cancel".

Example:

my_options = {"An item", "another", "yet another"};
[sel, ok] = listdlg ("ListString", my_options,
                     "SelectionMode", "Multiple");
if (ok == 1)
  disp ("You selected:");
  for i = 1:numel (sel)
    disp (sprintf ("\t%s", my_options{sel(i)}));
  endfor
else
  disp ("You cancelled.");
endif

See also: menu, errordlg, helpdlg, inputdlg, msgbox, questdlg, warndlg.

Demonstration 1

The following code

 disp ("- test listdlg with selectionmode single. No caption, no prompt.");
 itemlist = {"An item \\alpha", "another", "yet another"};
 s = listdlg ("ListString", itemlist, "SelectionMode", "Single");
 imax = numel (s);
 for i=1:1:imax
   disp (["Selected: ", num2str(i), ": ", itemlist{s(i)}]);
 endfor

Produces the following output

- test listdlg with selectionmode single. No caption, no prompt.
Selected: 1: An item \alpha

Demonstration 2

The following code

 disp ("- test listdlg with selectionmode and preselection. Has caption and two lines prompt.");
 itemlist = {"An item \\alpha", "another", "yet another"};
 s = listdlg ("ListString", itemlist, ...
              "SelectionMode", "Multiple", ...
              "Name", "Selection Dialog", ...
              "InitialValue", [1,2,3,4],
              "PromptString", {"Select an item...", "...or multiple items"});
 imax = numel (s);
 for i=1:1:imax
   disp (["Selected: ", num2str(i), ": ", itemlist{s(i)}]);
 endfor

Produces the following output

- test listdlg with selectionmode and preselection. Has caption and two lines prompt.
Selected: 1: An item \alpha
Selected: 2: another
Selected: 3: yet another

Demonstration 3

The following code

 disp ("- test listdlg with listsize.");
 itemlist = {"Neutron", "Electron", "Quark", "Proton", "Neutrino"};
 s = listdlg ("ListString", itemlist,
              "Name", "Bits and Pieces",
              "ListSize", [200 75]);
 imax = numel (s);
 for i=1:1:imax
   disp (["Selected: ", num2str(i), ": ", itemlist{s(i)}]);
 endfor

Produces the following output

- test listdlg with listsize.
Selected: 1: Neutron

Package: octave