• Matlab TutorialsNo Comments

    default thumbnail

    Running the Simulation from the GUI

    The GUI Simulate and store results button callback runs the model simulation and stores the results in the handles structure. Storing data in the handles structure simplifies the process of passing data to other subfunction since this structure can be passed as an argument.

    When a user clicks the Simulate and store results button, the callback executes the following steps:

    • Calls sim, which runs the simulation and returns the data that is used for plotting.
    • Creates a structure to save the results of the simulation, the current values of the simulation parameters set by the GUI, and the run and number.
    • Stores the structure in the handles structure.
    • Updates the String to list the most recent run.

    Here is the Simulate and store results button callback:

    function SimulateButton_Callback(hObject, eventdata, handles)
    [timeVector,stateVector,outputVector] = sim('f14');
    % Retrieve old results data structure
    if isfield(handles,'ResultsData') & 
    	ResultsData = handles.ResultsData;
    	% Determine the  run number currently used.
    	maxNum = ResultsData(length(ResultsData)).RunNumber;
    	ResultNum = maxNum+1;
    else % Set up the results data structure
    	ResultsData = struct('RunName',[],'RunNumber',[],...
    	ResultNum = 1;
    if isequal(ResultNum,1),
    	%  the Plot and Remove buttons
    %  Ki and Kf values to store with the data and put in the 
    results list.
    Ki = (handles.KiValueSlider,'Value');
    Kf = (handles.KfValueSlider,'Value');
    ResultsData(ResultNum).RunName = ['Run',num2str(ResultNum)];
    ResultsData(ResultNum).RunNumber = ResultNum;
    ResultsData(ResultNum).KiValue = Ki;
    ResultsData(ResultNum).KfValue = Kf;
    ResultsData(ResultNum).timeVector = timeVector;
    ResultsData(ResultNum).outputVector = outputVector;
    % Build the new results list string for the listbox
    ResultsStr = (handles.ResultsList,'String');
    if isequal(ResultNum,1)
    	ResultsStr = {['Run1',num2str(Kf),' ',num2str(Ki)]};
    	ResultsStr = [ResultsStr;...
    	{['Run',num2str(ResultNum),' ',num2str(Kf),' ', ...
    % Store the new ResultsData
    handles.ResultsData = ResultsData;
    guidata(hObject, handles)

    Removing Results from the List Box

    The GUI Remove button callback deletes any selected item from the Results list list box. It also deletes the corresponding run data from the handles structure. When a user clicks the Remove button, the callback executes the following steps:

    • Determines which list box items are selected when a user clicks the Remove button and removes those items from the list box String property by setting each item to the empty matrix [].
    • Removes the deleted data from the handles structure.
    • Displays the string and disables the Remove and Plot buttons (using the Enable property), if all the items in the list box are removed.
    • Save the changes to the handles structure (guidata).

    Here is the Remove button callback:

    function RemoveButton_Callback(hObject, eventdata, handles)
    currentVal = get(handles.ResultsList,'Value');
    resultsStr = get(handles.ResultsList,'String');
    numResults = size(resultsStr,1);
    % Remove the data and list entry for the selected value
    resultsStr(currentVal) =[];
    % If there are no other entries, disable the Remove and Plot 
    % and change the list string to 
    if isequal(numResults,length(currentVal)),
    	resultsStr = {''};
    	currentVal = 1;
    % Ensure that list box Value is valid, then reset Value and String
    currentVal = min(currentVal,size(resultsStr,1));
    % Store the new ResultsData
    guidata(hObject, handles)

    Plotting the Results Data

    The GUI Plot button callback creates a plot of the run data and adds a legend. The data to plot is passed to the callback in the handles structure, which also contains the gain settings used when the simulation ran. When a user clicks the Plot button, the callback executes the following steps:

    • Collects the data for each run selected in the Results list, including two variables (time vector and output vector) and a color for each result run to plot.
    • Generates a string for the legend from the stored data.
    • Creates the figure and axes for plotting and saves the handles for use by the Close button callback.
    • Plots the data, adds a legend, and makes the figure visible.

    The figure that contains the plot is created as invisible and then made visible after adding the plot and legend. To prevent this figure from becoming the target for plotting commands issued at the command line or by other GUIs, its HandleVisibility and properties are set to off. This means the figure is also hidden from the plot and legend commands.

    Use the following steps to plot into a hidden figure:

    • Save the handle of the figure when you create it.
    • Create an axes, set its Parent property to the figure handle, and save the axes handle.
    • Create the plot (which is one or more line objects), save these line handles, and set their Parent properties to the handle of the axes.
    • Make the figure visible.

    Plot Button Callback Listing

    Here is the Plot button callback.

    function PlotButton_Callback(hObject, eventdata, handles)
    currentVal = get(handles.ResultsList,'Value');
    % Get data to plot and generate command string with color 
    % specified
    legendStr = cell(length(currentVal),1);
    plotColor = {'b','g','r','c','m','y','k'};
    for ctVal = 1:length(currentVal);
    	PlotData{(ctVal*3)-2} = 
    	PlotData{(ctVal*3)-1} = 
    	numColor = ctVal - 7*( floor((ctVal-1)/7) );
    	PlotData{ctVal*3} = plotColor{numColor};
    	legendStr{ctVal} = ...
          [handles.ResultsData(currentVal(ctVal)).RunName,'; Kf=',...
          ';  Ki=', ...
    % If necessary, create the plot figure and store in handles
    % structure
    if ~isfield(handles,'PlotFigure') ||...
    	handles.PlotFigure = ...
            figure('Name','F14 Simulation Output',...
    	handles.PlotAxes = axes('Parent',handles.PlotFigure);
    	guidata(hObject, handles)
    % Plot data
    pHandles = plot(PlotData{:},'Parent',handles.PlotAxes);
    % Add a legend, and bring figure to the front
    % Make the figure visible and bring it forward

    The GUI Help Button

    The GUI Help button callback displays an HTML file in the MATLAB Help browser. It uses two commands:

    • The which command returns the full to the file when it is on the MATLAB
    • The web command displays the file in the Help browser.

    This is the Help button callback.

    function HelpButton_Callback(hObject, eventdata, handles)
    HelpPath = which('f14ex_help.html');

    You can also display the help document in a Web browser or load an external URL. For a description of these options, see the for web.

    Closing the GUI

    The GUI Close button callback closes the plot figure, if one exists and then closes the GUI. The handle of the plot figure and the GUI figure are available from the handles structure. The callback executes two steps:

    • Checks to see if there is a PlotFigure field in the handles structure and if it contains a valid figure handle (the user could have closed the figure manually).
    • Closes the GUI figure.

    This is the Close button callback:

    function CloseButton_Callback(hObject, eventdata, handles)
    % Close the GUI and any plot window that is open
    if isfield(handles,'PlotFigure') && ...

    The List Box Callback and Create Function

    This GUI does not use the list box callback because the actions performed on list box items are carried out by push buttons (Simulate and store results, Remove, and Plot). GUIDE automatically inserts a callback stub when you add the list box and automatically sets the Callback property to execute this subfunction whenever the callback is triggered (which happens when users select an item in the list box).

    In this example, there is no need for the list box callback to execute. You can it from the GUI code file and at the same time also the Callback property string in the Property Inspector so that the software does not attempt to execute the callback.

    [important]For more information on how to trigger the list box callback, see the description of list box.[/important]

    Setting the Background to White

    The list box create function enables you to determine the background color of the list box. The following code shows the create function for the list box that is tagged ResultsList:

    function ResultsList_CreateFcn(hObject, eventdata, handles)
    % Hint: listbox controls usually have a white background, change
    %       'usewhitebg' to 0 to use default. See ISPC and COMPUTER.
    usewhitebg = 1;
    if usewhitebg
    You might also be interested in :  Matlab Using FFT

    Comment closed!