In this part of the tutorial, we are going to code the GUI so that it can call the Simulink Model.

The GUI portion Method 1: using simset

The simset command allows you to define which workspace to run your simulink model from. By default, the simulink model is run from is the main workspace.

  1. Type guide at the command prompt.

    Command prompt

  2. Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

    GUIDE Screen

  3. Here is what the GUI should look like when you open it:

    GUI Figure

  4. The following is the code for the simulate_pushbutton_Callback functon.

    axes(handles.axes1) %set the axes
    m=str2num(get(handles.mass_editText,'String')); %fetch the mass value
    c=str2num(get(handles.damping_editText,'String')); %fetch the damping value
    k=str2num(get(handles.spring_editText,'String')); %fetch the spring constant
     
    %configure the options so that the current 
    %workspace is used in simulating the model
    options = simset('SrcWorkspace','current');
     
    %the sim command simulates the simulink model
    %the first argument is the model name
    %the second argument is an array containing the start and stop time
    %if [] is used, then the value from within the simulink model is used
    %the third argument is the options configured from simset
    sim('mass_spring',[],options);
    %plot the data
    plot(tout,yout)
    xlabel('Time')
    ylabel('Displacement')
    Title('2nd Order Mass Spring System')
    grid on
  5. Now, we are ready to run the GUI. You should see the following GUI appear. Try inputting different parameters and simulating the system response. Be sure the parameters are well defined (no letters, no symbols, no negative numbers, etc) or an error will result.

    Run the GUI

The GUI portion Method 2

This method is not as elegant as the previous method, but it is how I first learned how to run simulink models within GUIs.

  1. Type guide at the command prompt.

    Command prompt

  2. Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

    GUIDE Screen

  3. Here is what the GUI should look like when you open it:

    GUI Figure

  4. Double click on the “Simulate” Button to bring up the Property Inspector. Change the Callback property to simulateButton. Now, each time the “Simulate” button is pressed, the callback is mapped to the simulateButton m-file instead of the normal callback.

    Change the Callback

  5. The following is the code for simulateButton.m. This file is included in the zip folder you downloaded earlier. Notice that this m-file is NOT in function format. Instead, all of these commands are being executed in the main workspace area! Since all the parameters are defined in the main workspace, when we use the sim command to simulate the model, the variables m,c,and f are defined!! This would not have been possible if a normal callback was used.

    %notice that this m-file is NOT a function because simulink models
    %only allows you to use variables that are within the main workspace
    clear all
     
    %make the handles structures available to the main workspace
    h =gcf;
    handles = guidata(h);
     
    %set the axes to which the data will be plotted to
    axes(handles.axes1);
     
    % get the parameters from the edit text fields
    m=str2num(get(handles.mass_editText,'String'));
    c=str2num(get(handles.damping_editText,'String'));
    k=str2num(get(handles.spring_editText,'String'));
     
    %simulate the system 
    sim('mass_spring');
     
     
    %plot the data
    plot(tout,yout)
    xlabel('Time')
    ylabel('Displacement')
    Title('2nd Order Mass Spring System')
    grid on
  6. Now, we are ready to run the GUI. You should see the following GUI appear. Try inputting different parameters and simulating the system response. Be sure the parameters are well defined (no letters, no symbols, no negative numbers, etc) or an error will result.

    Run the GUI

The next section will discuss how to configure other Simulink parameters such as the sources, sinks, time steps, simulation time, and some other things.

Pages: 1 2 3 4