Creating Graphical User Interfaces |
Adding Code to the Callbacks
When the GUI is completed and running, and a user clicks a component of the GUI, MATLAB executes the callback specified by the component's Callback
property. The name of the callback is determined by the component's tag
property. For example, the callback for the Surf push button is surf_pushbutton_Callback
.
The following section describes how to add the code for the callbacks.
Push Button Callbacks
Each of the push buttons creates a different type of plot using the data specified by the current selection in the pop-up menu. Their callbacks get data from the handles
structure and then plot it. To add code to the surf push button callback, click surf_pushbutton_Callback
in the callback pop-up menu.
Add the code after the comments following the function definition, as shown below:
% --- Executes on button press in surf_pushbutton.
function surf_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to surf_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Display surf plot of the currently selected data
surf(handles.current_data);
You can add similar code to the Mesh and Contour push button callbacks after the autogenerated code.
Add this code to the Mesh push button callback:
Add this code to the Contour push button callback:
Pop-up Menu Callback
The pop-up menu enables users to select the data to plot. Every time a user selects one of the three plots, the pop-up menu callback reads the pop-up menu Value
property to determine what item is currently displayed and sets handles.current_data
accordingly. Add the following code to the plot_popup_Callback
after the comments following the function definition.
% --- Executes on selection change in data_popup.
function plot_popup_Callback(hObject, eventdata, handles)% hObject handle to surf_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
val = get(hObject,'Value'); str = get(hObject, 'String'); switch str{val}; case 'peaks'% User selects peaks
handles.current_data = handles.peaks; case 'membrane'% User selects membrane
handles.current_data = handles.membrane; case 'sinc'% User selects sinc
handles.current_data = handles.sinc; end guidata(hObject,handles)
Adding Code to the Opening Function | Using the Object Browser to Identify Callbacks |