| MATLAB® | ![]() |
| On this page… |
|---|
Techniques Used in the Example |
This example creates a GUI that contains two axes for plotting data. For simplicity, this example obtains data by evaluating an expression using parameters entered by the user.

GUI-building techniques illustrated in this example include
Controlling which axes is the target for plotting commands.
Using edit text controls to read numeric input and MATLAB® expressions.
If you are reading this in the MATLAB Help browser, you can click the following links to display the GUIDE Layout Editor and the MATLAB Editor with a completed version of this example. This enables you to see the values of all component properties and to understand how the components are assembled to create the GUI. You can also see a complete listing of the code that is discussed in the following sections.
Note The following links execute MATLAB commands and are designed to work within the MATLAB Help browser. If you are reading this online or in PDF, you should go to the corresponding section in the MATLAB Help Browser to use the links. |
This GUI requires three input values:
Frequency one (f1)
Frequency two (f2)
A time vector (t)
When the user clicks the Plot button, the GUI puts these values into a MATLAB expression that is the sum of two sine function:
x = sin(2*pi*f1*t) + sin(2*pi*f2*t)
The GUI then calculates the FFT of x and creates two plots — one frequency domain and one time domain.
The GUI uses default values for the three inputs. This enables users to click on the Plot button and see a result as soon as the GUI is run. It also helps to indicate what values the user might enter.
To create the default values, set the String property of the edit text. The following figure shows the value set for the time vector.

Since there are two axes in this GUI, you must be able to specify which one you want to target when you issue the plotting commands. To do this, use the handles structure, which contains the handles of all components in the GUI.
The field name in the handles structure that contains the handle of any given component is derived from the component's Tag property. To make code more readable (and to make it easier to remember) this example sets the Tag property to descriptive names.

For example, the Tag of the axes used to display the FFT is set to frequency_axes. Therefore, within a callback, you access its handle with
handles.frequency_axes
Likewise, the Tag of the time axes is set to time_axes.
See handles Structure for more information on the handles structure. See Plot Push Button Callback for the details of how to use the handle to specify the target axes.
There are two GUI option settings that are particularly important for this GUI:
Resize behavior: Proportional
Command-line accessibility: Callback
Proportional Resize Behavior. Selecting Proportional as the resize behavior enables users to change the GUI to better view the plots. The components change size in proportion to the GUI figure size. This generally produces good results except when extremes of dimensions are used.
Callback Accessibility of Object Handles. When GUIs include axes, handles should be visible from within callbacks. This enables you to use plotting commands like you would on the command line. Note that Callback is the default setting for command-line accessibility.
See GUI Options for more information.
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.
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:
Reading the current values in the three edit text boxes using the handles structure to access the edit text handles.
Converting the two frequency values (f1 and f2) from string to doubles using str2double.
Evaluating the time string using 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'));
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.
The final task for the callback is to actually generate the plots. This involves
Making the appropriate axes current using the axes command and the handle of the axes. For example,
axes(handles.frequency_axes)
Issuing the plot command.
Setting any properties that are automatically reset by the 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
![]() | Examples of GUIDE GUIs | List Box Directory Reader | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |