| Creating Graphical User Interfaces | ![]() |
Programming the Slider and Edit Text Components
This GUI employs a useful combination of components in its design. Each slider is coupled to an edit text component so that:
Slider Callback
The GUI uses two sliders to specify block gains since 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:
set_param).
Here is the callback for the Proportional (Kf) slider.
function KfValueSlider_Callback(hObject, eventdata, handles)% Ensure model is openmodel_open(handles)% Get the new value for the Kf Gain from the sliderNewVal = get(hObject, 'Value');% Set the value of the KfCurrentValue to the new value set by sliderset(handles.KfCurrentValue,'String',NewVal)% Set the Gain parameter of the Kf Gain Block to the new valueset_param('f14/Controller/Gain','Gain',num2str(NewVal))
Note that, 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 a similar approach.
Current Value Edit Text Callback
The edit text box enables users to type in a value for the respective parameter. When the user clicks on another component in the GUI after typing into the text box, the edit text callback executes the following steps:
String property to a double (str2double).
If the value is out of range, the edit text String property is set to the value of the slider (rejecting the number typed in by the user).
If the value is in range, the slider Value property is updated 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 openmodel_open(handles)% Get the new value for the Kf GainNewStrVal = get(hObject, 'String'); NewVal = str2double(NewStrVal);% Check that the entered value falls within the allowable rangeif isempty(NewVal) | (NewVal< -5) | (NewVal>0),% Revert to last value, as indicated by KfValueSliderOldVal = get(handles.KfValueSlider,'Value'); set(hObject, 'String',OldVal) else,% Use new Kf value% Set the value of the KfValueSlider to the new valueset(handles.KfValueSlider,'Value',NewVal)% Set the Gain parameter of the Kf Gain Block to the new valueset_param('f14/Controller/Gain','Gain',NewStrVal) end
The callback for the Ki Current value follows a similar approach.
| Running the GUI | Running the Simulation from the GUI | ![]() |
© 1994-2005 The MathWorks, Inc.