Main Content

Build GUI With Interactive Response-Plot Updates

This example shows how to create a GUI to display a Control System Toolbox™ response plot that changes in response to interactive input.

The GUI in this example displays the step response of a second-order dynamic system of fixed natural frequency. The GUI includes a slider that sets the system’s damping ratio. To cause the response plot to reflect the slider setting, you must define a callback for the slider. This callback uses the updateSystem command to update the plot with new system data in response to changes in the slider setting.

Set the initial values of the second-order dynamic system and create the system model.

zeta = .5;                           % Damping Ratio
wn = 2;                              % Natural Frequency
sys = tf(wn^2,[1,2*zeta*wn,wn^2]); 

Create a figure for the GUI and configure the axes for displaying the step response.

f = figure;
ax = axes('Parent',f,'position',[0.13 0.39  0.77 0.54]);
h = stepplot(ax,sys);
setoptions(h,'XLim',[0,10],'YLim',[0,2]);

Add the slider and slider label text to the figure.

b = uicontrol('Parent',f,'Style','slider','Position',[81,54,419,23],...
              'value',zeta, 'min',0, 'max',1);
bgcolor = f.Color;
bl1 = uicontrol('Parent',f,'Style','text','Position',[50,54,23,23],...
                'String','0','BackgroundColor',bgcolor);
bl2 = uicontrol('Parent',f,'Style','text','Position',[500,54,23,23],...
                'String','1','BackgroundColor',bgcolor);
bl3 = uicontrol('Parent',f,'Style','text','Position',[240,25,100,23],...
                'String','Damping Ratio','BackgroundColor',bgcolor);

Set the callback that updates the step response plot as the damping ratio slider is moved.

b.Callback = @(es,ed) updateSystem(h,tf(wn^2,[1,2*(es.Value)*wn,wn^2])); 

This code sets the callback for the slider (identified as b) to an anonymous function. The input arguments to this anonymous function, es and ed, are automatically passed to the callback when the slider is used. es is the handle of the uicontrol that represents the slider, and ed is the event data structure which the slider automatically passes to the callback. You do not need to define these variables in the workspace or set their values. (For more information about UI callbacks, see Callback Definition.)

The callback is a call to the updateSystem function, which replaces the plotted response data with a response derived from a new transfer function. The callback uses the slider data es.Value to define a second-order system whose damping ratio is the current value of the slider.

Now that you have set the callback, move the slider. The displayed step response changes as expected.

See Also

|

Related Topics