| Contents | Index |
| On this page… |
|---|
When you saved your GUI in the previous topic, Save the GUI Layout, GUIDE created two files: a FIG-file simple_gui.fig that contains the GUI layout and a file, simple_gui.m, that contains the code that controls how the GUI behaves. The code consists of a set of MATLAB functions (that is, it is not a script). But the GUI did not respond because the functions contain no statements that perform actions yet. This topic shows you how to add code to the file to make the GUI do things. The following three sections describe the steps to take:
This topic shows you how to generate the data to be plotted when the GUI user clicks a button. The opening function generates this data by calling MATLAB functions. The opening function, which initializes a GUI when it opens, is the first callback in every GUIDE-generated GUI code file.
In this example, you add code that creates three data sets to the opening function. The code uses the MATLAB functions peaks, membrane, and sinc.
Display the opening function in the MATLAB editor.
If the file simple_gui.m is not already open in
your editor, open it by selecting Editor from
the GUIDE View menu. In the editor,
click the function icon
on the toolbar, then select simple_gui_OpeningFcn in the pop-up menu that
displays.

The cursor moves to the opening function, which already contains this code:
% --- Executes just before simple_gui is made visible. function simple_gui_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to simple_gui (see VARARGIN) % Choose default command line output for simple_gui handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes simple_gui wait for user response (see UIRESUME) % uiwait(handles.figure1);
Create data for the GUI to plot by adding the following code to the opening function immediately after the comment that begins % varargin...
% Create the data to plot. handles.peaks=peaks(35); handles.membrane=membrane; [x,y] = meshgrid(-8:.5:8); r = sqrt(x.^2+y.^2) + eps; sinc = sin(r)./r; handles.sinc = sinc; % Set the current data value. handles.current_data = handles.peaks; surf(handles.current_data)
The first six executable lines create the data using the MATLAB functions peaks, membrane, and sinc. They store the data in the handles structure, an argument provided to all callbacks. Callbacks for the push buttons can retrieve the data from the handles structure.
The last two lines create a current data value and set it to peaks, and then display the surf plot for peaks. The following figure shows how the GUI now looks when it first displays.

The pop-up menu enables the user to select the data to plot. When the GUI user selects one of the three plots, MATLAB software sets the pop-up menu Value property to the index of the selected string. The pop-up menu callback reads the pop-up menu Value property to determine the item that the menu currently displays , and sets handles.current_data accordingly.
Display the pop-up menu callback in the MATLAB Editor. Right-click the pop-up menu component in the GUIDE Layout Editor to display a context menu. From that menu, select View Callbacks > Callback.

If the editor is not already open, GUIDE opens it to display the GUI code file, and the cursor moves to the pop-menu callback, which already contains this code:
% --- Executes on selection change in popupmenu1. 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)
Add the following code to the popupmenu1_Callback after the comment that begins % handles...
This code first retrieves two pop-up menu properties:
String — a cell array that contains the menu contents
Value — the index into the menu contents of the selected data set
It then uses a switch statement to make the selected data set the current data. The last statement saves the changes to the handles structure.
% Determine the selected data set.
str = get(hObject, 'String');
val = get(hObject,'Value');
% Set current data to the selected data set.
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
% Save the handles structure.
guidata(hObject,handles)Each of the push buttons creates a different type of plot using the data specified by the current selection in the pop-up menu. The push button callbacks get data from the handles structure and then plot it.
Display the Surf push button callback in the MATLAB Editor. Right-click the Surf push button in the Layout Editor to display a context menu. From that menu, select View Callbacks > Callback.

In the editor, the cursor moves to the Surf push button callback in the GUI code file, which already contains this code:
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
Add the following code to the callback immediately after the comment that begins % handles...
% Display surf plot of the currently selected data. surf(handles.current_data);
Repeat steps 1 and 2 to add similar code to the Mesh and Contour push button callbacks.
Add this code to the Mesh push button callback, pushbutton2_Callback:
% Display mesh plot of the currently selected data. mesh(handles.current_data);
Add this code to the Contour push button callback, pushbutton3_Callback:
% Display contour plot of the currently selected data. contour(handles.current_data);
Your GUI is ready to run. The next topic, Open and Run the GUI, tells you how to do that.
![]() | Save the GUI Layout | Use the Completed Simple GUIDE GUI | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |