Write the Callback Function
This code is an example of a list box callback function in GUIDE. Associate this function
with the list box Callback property to make it execute when a selects an item in the list
box.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns contents
% contents{get(hObject,'Value')} returns selected item from listbox1
items = get(hObject,'String');
index_selected = get(hObject,'Value');
item_selected = items{index_selected};
display(item_selected);
When the end user selects an item in the list box, the callback function performs the
following tasks:
- Gets all the items in the list box and stores them in the variable, items.
- Gets the numeric index of the selected item and stores it in the variable,
index_selected. - Gets the value of the selected item and stores it in the variable, item_selected.
- Displays the selected item in the MATLAB Command Window.
The example, “Interactive List Box App in GUIDE” on page 8-15 shows how to populate a
list box with directory names.
Pop-Up Menu
Populate Items in the Pop-Up Menu
If you are developing an app using GUIDE, use the pop-up menu CreateFcn callback to
add items to the pop-up menu.
This code is an example of a pop-up menu CreateFcn callback that populates the menu
with the items, Red, Green, and Blue.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
7 Programming a GUIDE App