MATLAB Creating Graphical User Interfaces

(ff) #1
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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...
items = get(hObject,'String');
index_selected = get(hObject,'Value');
item_selected = items{index_selected};
display(item_selected);

When the user selects an item in the pop-up menu, the callback function performs the
following tasks:


  • Gets all the items in the pop-up menu 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.


Panel


Make the Panel Respond to Button Clicks

You can create a callback function that executes when the end user right-clicks or left-
clicks on the panel. If you are working in GUIDE, then right-click the panel in the layout
and select View Callbacks > ButtonDownFcn to create the callback function.

This code is an example of a ButtonDownFcn callback in GUIDE.

function uipanel1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to uipanel1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
display('Mouse button was pressed');

When the end user clicks on the panel, this function displays the text, 'Mouse button
was pressed', in the Command Window.

7 Programming a GUIDE App

Free download pdf