MATLAB Creating Graphical User Interfaces

(ff) #1

% handles empty - handles not created until after all CreateFcns


% Hint: popupmenu controls usually have a white background on Windows.
if ispc && isequal(get(hObject,'BackgroundColor'),...
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
set(hObject,'String',{'Red';'Green';'Blue'});


The last line, set(hObject,'String',{'Red';'Green';'Blue'}), populates the
contents of the pop-up menu.


If you are developing an app programmatically (without GUIDE), then populate the pop-
up menu when you create it. For example:


function myui()
figure
uicontrol('Style','popupmenu',...
'String',{'Red';'Green';'Blue'},...
'Position',[40 70 80 20]);
end


Change the Selected Item


When the end user selects an item, the pop-up menu’s Value property changes to a
number that corresponds to the item’s position in the menu. For example, a value of 1
corresponds to the first item in the list. If you want to change the selection in your code,
then change the Value property to another number between 1 and the number of items
in the menu.


For example, you can use the handles structure in GUIDE to access the pop-up menu
and change the Value property:


set(handles.popupmenu1,'Value',2)


The first argument, handles.popupmenu1, might be different in your code, depending
on the value of the pop-up menu Tag property.


Write the Callback Function


This code is an example of a pop-up menu callback function in GUIDE. Associate this
function with the pop-up menu Callback property to make it execute when the end user
selects an item from the menu.


Callbacks for Specific Components
Free download pdf