Creating Graphical User Interfaces |
Plot Push Button Callback
This GUI uses only the Plot button callback; the edit text callbacks are not needed and have been deleted from the GUI M-file. When a user clicks the Plot button, the callback performs three basic tasks - it gets user input from the edit text components, calculates data, and creates the two plots.
Getting User Input
The three edit text boxes provide a way for the user to enter values for the two frequencies and the time vector. The first task for the callback is to read these values. This involves:
handles
structure to access the edit text handles.
f1
and f2
) from string to doubles using str2double
.
eval
to produce a vector t
, which the callback used to evaluate the mathematical expression.
The following code shows how the callback obtains the input.
% Get user input from GUI
f1 = str2double(get(handles.f1_input,'String'));
f2 = str2double(get(handles.f2_input,'String'));
t = eval(get(handles.t_input,'String'));
Calculating Data
Once the input data has been converted to numeric form and assigned to local variables, the next step is to calculate the data needed for the plots. See the fft
function for an explanation of how this is done.
Targeting Specific Axes
The final task for the callback is to actually generate the plots. This involves
axes
command and the handle of the axes. For example,
plot
command.
plot
command.
The last step is necessary because many plotting commands (including plot
) clear the axes before creating the graph. This means you cannot use the Property Inspector to set the XMinorTick
and grid properties that are used in this example, since they are reset when the callback executes plot
.
When looking at the following code listing, note how the handles
structure is used to access the handle of the axes when needed.
function plot_button_Callback(hObject, eventdata, handles)% hObject handle to plot_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get user input from GUI
f1 = str2double(get(handles.f1_input,'String')); f2 = str2double(get(handles.f2_input,'String')); t = eval(get(handles.t_input,'String'));% Calculate data
x = sin(2*pi*f1*t) + sin(2*pi*f2*t); y = fft(x,512); m = y.*conj(y)/512; f = 1000*(0:256)/512;;% Create frequency plot
axes(handles.frequency_axes)% Select the proper axes
plot(f,m(1:257)) set(handles.frequency_axes,'XMinorTick','on') grid on% Create time plot
axes(handles.time_axes)% Select the proper axes
plot(t,x) set(handles.time_axes,'XMinorTick','on') grid on
Design of the GUI | List Box Directory Reader |