GUI with Multiple Axes

Multiple Axes Example Outcome

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.

Signal analysis GUI

Techniques Used in the Example

GUI-building techniques illustrated in this example include

View Completed Layout and Its GUI M-File

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.

Design of the GUI

This GUI requires three input values:

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.

Specifying Default Values for the Inputs

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.

property inspector showing string property highlighted

Identifying the Axes

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.

Tag property set to frequency axes

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.

GUI Option Settings

There are two GUI option settings that are particularly important for this GUI:

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.

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:

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

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.

Plot Button Code Listing

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
  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS