Main Content

GUIDE App with Parameters for Displaying Plots

Note

The GUIDE environment will be removed in a future release. After GUIDE is removed, existing GUIDE apps will continue to run in MATLAB® but they will not be editable in GUIDE.

To continue editing an existing GUIDE app, see GUIDE Migration Strategies for information on how to help maintain compatibility of the app with future MATLAB releases. To create new apps interactively, Develop Apps Using App Designer instead.

This example shows how to examine and run a prebuilt GUIDE app. The app contains three edit fields and two axes. The axes display the frequency and time domain representations of a function that is the sum of two sine waves. The top two edit fields contain the frequency for each component sine wave. The third edit field contains the time range and sampling rate for the plots.

Open and Run the Example

Open and run the app. Change the default values in the f1 and f2 fields to change the frequency for each component sine wave. You can also change the three numbers (separated by colons) in the t field. The first and last numbers specify the window of time to sample the function. The middle number specifies the sampling rate.

Press the Plot button to see the graph of the function in the frequency and time domains.

Examine the Code

  1. In GUIDE, click the Editor button to view the code.

  2. Near the top of the Editor window, use the Go To button to navigate to the functions discussed below.

f1_input_Callback and f2_input_Callback

The f1_input_Callback function executes when the user changes the value in the f1 edit field. The f2_input_Callback function responds to changes in the f2 field, and it is almost identical to the f1_input_Callback function. Both functions check for valid user input. If the value in the edit field is invalid, the Plot button is disabled. Here is the code for the f1_input_Callback function.

f1 = str2double(get(hObject,'String'));
if isnan(f1) || ~isreal(f1)
    % Disable the Plot button and change its string to say why
    set(handles.plot_button,'String','Cannot plot f1');
    set(handles.plot_button,'Enable','off');
    % Give the edit text box focus so user can correct the error
    uicontrol(hObject);
else 
    % Enable the Plot button with its original name
    set(handles.plot_button,'String','Plot');
    set(handles.plot_button,'Enable','on');
end

t_input_Callback

The t_input_Callback function executes when the user changes the value in the t edit field. This try block checks the value to make sure that it is numeric, that its length is between 2 and 1000, and that the vector is monotonically increasing.

try
    t = eval(get(handles.t_input,'String'));
    if ~isnumeric(t)
        % t is not a number
        set(handles.plot_button,'String','t is not numeric')
    elseif length(t) < 2
        % t is not a vector
        set(handles.plot_button,'String','t must be vector')
    elseif length(t) > 1000
        % t is too long a vector to plot clearly
        set(handles.plot_button,'String','t is too long')
    elseif min(diff(t)) < 0
        % t is not monotonically increasing
        set(handles.plot_button,'String','t must increase')
    else
        % Enable the Plot button with its original name
        set(handles.plot_button,'String','Plot')
        set(handles.plot_button,'Enable','on')
        return
    end

 catch EM
    % Cannot evaluate expression user typed
    set(handles.plot_button,'String','Cannot plot t');
    uicontrol(hObject);
end
The catch block changes the label on the Plot button to indicate that an input value was invalid. The uicontrol command sets the focus to the field that contains the erroneous value.

plot_button_Callback

The plot_button_Callback function executes when the user clicks the Plot button.

First, the callback gets the values in the three edit fields:

f1 = str2double(get(handles.f1_input,'String'));
f2 = str2double(get(handles.f2_input,'String'));
t = eval(get(handles.t_input,'String'));
Then callback uses values of f1, f2, and t to sample the function in the time domain and calculate the Fourier transform. Then, the two plots are updated:

% Create frequency plot in proper axes
plot(handles.frequency_axes,f,m(1:257));
set(handles.frequency_axes,'XMinorTick','on');
grid(handles.frequency_axes,'on');

% Create time plot in proper axes
plot(handles.time_axes,t,x);
set(handles.time_axes,'XMinorTick','on');
grid on

Related Topics