This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

GUIDE App With Parameters for Displaying Plots

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

To open the example in GUIDE, click the Open Script button on this page. To run the app, click the green Run Figure button at the top of the GUIDE window.

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');
    % Give the edit text box focus so user can correct the error
    % Enable the Plot button with its original name


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.

    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')
        % Enable the Plot button with its original name

 catch EM
    % Cannot evaluate expression user typed
    set(handles.plot_button,'String','Cannot plot t');
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.


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
grid on

% Create time plot in proper axes
grid on

Related Examples

Was this topic helpful?