Creating Graphical User Interfaces |
Adding Code to the Opening Function
To create data for the GUI to plot, add the following code to the opening function immediately after the comments following the function declaration.
% --- 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 untitled (see 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; 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
to generate the data.
The next line, handles.current_data = handles.peaks
, sets the current_data
field of the handles structure equal to the data for peaks. The value of handles.current_data
changes each time a user selects a different plot from the pop-up menu - see Pop-up Menu Callback.
The last line displays the surf plot for peaks, which appears when the GUI is first opened.
GUIDE automatically generates two more lines of code in the opening function, which follow the code that you add:
handles.output = hObject
saves the handle to the GUI for later access by the output function. While this command is not necessary in this example, it is useful if you want to return the GUI handle to the command line.
guidata(hObject, handles)
saves the handles
structure.
Sharing Data Between Callbacks | Adding Code to the Callbacks |