• Uncategorised

A GUI to Set Simulink Model Parameters

About the Simulink Model Parameters Example

This example illustrates how to create a GUI that sets the parameters of a Simulink® model. In addition, the GUI can run the simulation and plot the results in a figure window. The following figure shows the GUI after running three simulations with different values for controller gains.

The example illustrates a number of GUI building techniques:

  • Opening and setting parameters on a Simulink model from a GUI.
  • Implementing sliders that operate in conjunction with text boxes, which display the current value, as well as accepting user input.
  • Enabling and disabling controls, depending on the state of the GUI.
  • Managing a variety of shared data using the handles structure.
  • Directing graphics output to figures with hidden handles.
  • Adding a Help button that displays .html files in the MATLAB Help browser.

View and Run the Simulink Parameters GUI

If you are reading this document in the MATLAB Help browser, you can access the example FIG-file and code file by clicking the following links. If you are reading this on the Web or in PDF form, go to the corresponding section in the MATLAB Help Browser to use the links.

If you intend to modify the layout or code of this GUI example, first save a copy of its code file and FIG-file to your current folder (you need write access to your current folder to do this). Follow these steps to copy the example files to your current folder and then to open them:

  • Click here to copy the files to your current folder.
  • Type guide f14ex or click here to open the FIG-file in GUIDE.
  • Type edit f14ex or click here to open the code file in the Editor.

You can view the properties of any component by double-clicking it in the Layout Editor to open the Property Inspector for it. You can modify either the figure, the code, or both, and then save the GUI in your current folder using File > Save as from GUIDE. This saves both files, allowing you to rename them, if you choose.

To just inspect the GUI in GUIDE and run it, follow these steps instead:

  • Click here to add the example files to the MATLAB path (only for the current session).
  • Click here to run the f14ex GUI.
  • Click here to display the GUI in the GUIDE Layout Editor (read only).
  • Click here to display the GUI code file in the MATLAB Editor (read only).
  • Do not save GUI files to the examples folder where you found them or you will overwrite the original files. If you want to save GUI files, use File > Save as from GUIDE, which saves both the GUI FIG-file and the GUI code file.

How to Use the Simulink Parameters GUI

You must have Simulink installed for this GUI to run. The first time you run the GUI, Simulink opens (if it is not already running) and loads the f14 demo model. This can take several seconds.

The GUI has a Help button. Clicking it opens an HTML file, f14ex_help.html, in the Help Browser. This file, which resides in the examples folder along with the GUI files, contains the following five sections of help text:

F14 Controller Gain Editor

You can use the F14 Controller Gain Editor to analyze how changing the gains used in the Proportional-Integral Controller affect the aircraft’s angle of attack and the amount of G force the pilot feels.

That the Simulink diagram f14.mdl must be open to run this GUI. If you close the F14 Simulink model, the GUI reopens it whenever it requires the model to execute.

Changing the Controller Gains

You can change gains in two blocks:

  • The Proportional gain (Kf) in the Gain block
  • The Integral gain (Ki) in the Transfer Function block

You can change either of the gains in one of the two ways:

  • Move the slider associated with that gain.
  • Type a new value into the Current value edit field associated with that gain.

The block’s values are updated as soon as you enter the new value in the GUI.

Running the Simulation

Once you have set the gain values, you can run the simulation by clicking the Simulate and store results button. The simulation time and output vectors are stored in the Results list.

Plotting the Results

You can generate a plot of one or more simulation results by selecting the row of results (Run1, Run2, etc.) in the Results list that you want to plot and clicking the Plot button. If you select multiple rows, the graph contains a plot of each result.

The graph is displayed in a figure, which is cleared each time you click the Plot button. The figure’s handle is hidden so that only the GUI can display graphs in this window.

Removing Results

To remove a result from the Results list, select the row or rows you want to remove and click the Remove button.

Running the GUI

The GUI is nonblocking and nonmodal because it is designed to be used as an analysis tool.

GUI Options Settings

This GUI uses the following GUI option settings:

  • Resize behavior: Non-resizable
  • Command-line accessibility: Off

GUI Options selected:

  • Generate callback function prototypes
  • GUI allows only one instance to run

Opening the Simulink Block Diagrams

This example is designed to work with the f14 Simulink model. Because the GUI sets parameters and runs the simulation, the f14 model must be open when the GUI is displayed. When the GUI runs, the model_open subfunction executes. The purpose of the subfunction is to:

  • Determine if the model is open (find_system).
  • Open the block diagram for the model and the subsystem where the parameters are being set, if not open already (open_system).
  • Change the size of the controller Gain block so it can display the gain value (set_param).
  • Bring the GUI forward so it is displayed on top of the Simulink diagrams (figure).
  • Set the block parameters to match the current settings in the GUI.

Here is the code for the model_open subfunction:

function model_open(handles)
if  isempty(find_system('Name','f14')),
	open_system('f14'); open_system('f14/Controller')
	set_param('f14/Controller/Gain','Position',[275 14 340 56])
	figure(handles.F14ControllerEditor)
	set_param('f14/Controller Gain','Gain',...
		get(handles.KfCurrentValue,'String'))
	set_param(...
      'f14/Controller/Proportional plus integral compensator',...
      'Numerator',...
      get(handles.KiCurrentValue,'String'))
end

Programming the Slider and Edit Text Components

In the GUI, each slider is coupled to an edit text component so that:

  • The edit text displays the current value of the slider.
  • The user can enter a value into the edit text box and cause the slider to update to that value.
  • Both components update the appropriate model parameters when activated by the user.

Slider Callback

The GUI uses two sliders to specify block gains because these components enable the selection of continuous values within a specified range. When a user changes the slider value, the callback executes the following steps:

  • Calls model_open to ensure that the Simulink model is open so that simulation parameters can be set.
  • Gets the new slider value.
  • Sets the value of the Current value edit text component to match the slider.
  • Sets the appropriate block parameter to the new value (set_param).

Here is the callback for the Proportional (Kf) slider:

function KfValueSlider_Callback(hObject, eventdata, handles)
% Ensure model is open.
model_open(handles)
% Get the new value for the Kf Gain from the slider.
NewVal = get(hObject, 'Value');
% Set the value of the KfCurrentValue to the new value 
% set by slider.
set(handles.KfCurrentValue,'String',NewVal)
% Set the Gain parameter of the Kf Gain Block to the new value.
set_param('f14/Controller/Gain','Gain',num2str(NewVal))

While a slider returns a number and the edit text requires a string, uicontrols automatically convert the values to the correct type.

The callback for the Integral (Ki) slider follows an approach similar to the Proportional (Kf) slider’s callback.

Current Value Edit Text Callback

The edit text box enables users to enter a value for the respective parameter. When the user clicks another component in the GUI after entering data into the text box, the edit text callback executes the following steps:

  • Calls model_open to ensure that the Simulink model is open so that it can set simulation parameters.
  • Converts the string returned by the edit box String property to a double (str2double).

Checks whether the value entered by the user is within the range of the slider:

If the value is out of range, the edit text String property is set to the value of the slider (rejecting the number entered by the user).

If the value is in range, the slider Value property is updated to the new value.

  • Sets the appropriate block parameter to the new value (set_param).

Here is the callback for the Kf Current value text box:

function KfCurrentValue_Callback(hObject, eventdata, handles)
% Ensure model is open.
model_open(handles)
% Get the new value for the Kf Gain.
NewStrVal = get(hObject, 'String');
NewVal = str2double(NewStrVal);
% Check that the entered value falls within the allowable range.
if  isempty(NewVal) || (NewVal< -5) || (NewVal>0),
   % Revert to last value, as indicated by KfValueSlider.
   OldVal = get(handles.KfValueSlider,'Value');
   set(hObject, 'String',OldVal)
else % Use new Kf value
   % Set the value of the KfValueSlider to the new value.
   set(handles.KfValueSlider,'Value',NewVal)
   % Set the Gain parameter of the Kf Gain Block 
   % to the new value.
   set_param('f14/Controller/Gain','Gain',NewStrVal)
end

The callback for the Ki Current value follows a similar approach.

You may also like...