
8 Programming the GUI
8-24
function listbox1_Callback(hObject, eventdata, handles)
index_selected = get(handles.listbox1,'Value');
list = get(handles.listbox1,'String');
item_selected = list{index_selected}; % Convert from cell array
% to string
Triggering Callback Execution
MATLAB executes the list box’s Callback callback after the mouse button is
released or after certain key press events:
• The arrow keys change the
Value property and trigger callback execution.
•
Enter and Space do not change the Value property but trigger callback
execution.
If the user double-clicks, the callback executes after each click. MATLAB sets
the figure’s
SelectionType property to normal on the first click and to open on
the second click. The callback can query the
SelectionType 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 10-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 10-16 — Shows
how to access variables in the MATLAB base workspace from a list box GUI.
Pop-Up Menu
When the pop-up menu Callback callback is triggered, the pop-up menu Value
property contains the index of the selected item, where
1 corresponds to the
first item on the menu. The
String property contains the menu items as a cell
array of strings.
Using Only the Index of the Selected Menu Item
This example retrieves only the index of the item selected. It uses a switch
statement to take action based on the value. If the contents of the pop-up menu
Komentarze do niniejszej Instrukcji