MATLAB Creating Graphical User Interfaces

(Barry) #1
UI for Setting Simulink Model Parameters

Slider Callback


The UI uses two sliders to specify block gains because these components enable the
selection of continuous values within a specified range. When you change the slider
value, the callback performs these tasks:



  • 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 you to enter a value for the respective parameter. When you
click another component in the UI after entering data into the text box, the edit text
callback performs these tasks:



  • 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 entered value is within the range of the slider:

Free download pdf