Specifications
4 Programming GUIs
4-12
callback can query the Selection Type property to determine if it was a single-
or double-click.
List Box Examples
See the following examples for more information on using list boxes:
• “List Box Directory Reader” on page 5-9 — Shows how to creates a GUI that
displays the contents of directories in a list box and enables users to open a
variety of file types by double-clicking on the filename.
• “Accessing Workspace Variables from a List Box” on page 5-15 — Shows how
to access variables in the MATLAB base workspace from a list box GUI.
Pop-Up Menus
You can program the popup menu callback to work by checking only the index
of the item selected (contained in the
Value property) or you can obtain the
actual string contained in the selected item.
This callback checks the index of the selected item and uses a switch statement
to take action based on the value. If the contents of the popup menu is fixed,
then you can use this approach.
function popupmenu1_Callback(hObject, eventdata, handles)
val = get(hObject,'Value');
switch val
case 1
% The user selected the first item
case 2
% The user selected the second item
% proceed with callback...
This callback obtains the actual string selected in the pop-up menu. It uses the
value to index into the list of strings. This approach may be useful if your
program dynamically loads the contents of the pop-up menu based on user
action and you need to obtain the selected string. Note that it is necessary to
convert the value returned by the
String property from a cell array to a string.
function popupmenu1_Callback(hObject, eventdata, handles)
val = get(hObject,'Value');
string_list = get(hObject,'String');
selected_string = string_list{val}; % convert from cell array
% to string