MATLAB Creating Graphical User Interfaces

(Barry) #1

8 Programming a GUIDE UI



  • Gets the numeric index of the selected item and stores it in the variable,
    index_selected.

  • Gets the string 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 in a GUIDE UI” on page 9-46 shows how to
populate a list box with directory names.

Pop-Up Menu


Populate Items in the Pop-Up Menu

If you are developing a UI 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
% 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 a UI 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
Free download pdf