Automatically Refresh Plot in a GUIDE GUI

About the Example

This example shows how to refresh a display by incorporating a timer in a GUI that updates data. Timers are MATLAB® objects. Programs use their properties and methods to schedule tasks, update information, and time out processes. For example, you can set up a timer to acquire real-time data at certain intervals, which your GUI then analyzes and displays.

The GUI displays a surface plot of the peaks function and contains three uicontrols:

  • The Start Randomizing push button — Starts the timer running, which executes at a rate determined by the slider control. At each iteration, random noise is added to the surface plot.

  • The Stop Randomizing push button — Halts the timer, leaving the surface plot in its current state until you click the Start Randomizing button again.

  • The Timer Period slider — Speeds up and slows down the timer, changing its period within a range of 0.01 to 2 seconds.

To get and view the example code:

  1. Copy the example FIG-file and code file to your current (writeable) folder and open the FIG-file in GUIDE with the following commands:

    copyfile(fullfile(docroot, 'techdoc','creating_guis','examples',...
    'ex_guide_timergui*.*')), fileattrib('ex_guide_timergui*.*', '+w')
    guide ex_guide_timergui.fig
    
  2. From the GUIDE Layout Editor, click the Editor button .

    The ex_guide_timergui.m code displays in the MATLAB Editor.

The following figures show the GUI when you run it. The left figure shows the initial state of the GUI. The right figure shows the GUI after running the timer for 5 seconds at its default period of 1 count per second.

For details about timer properties, methods, and events, see Use a MATLAB Timer Object and the timer reference page.

How the GUI Implements the Timer

Each callback in the GUI either creates, modifies, starts, stops, or destroys the timer object. The following sections describe what each callbacks does.

ex_guide_timergui_OpeningFcn

ex_guide_timergui_OpeningFcn creates the timer using the following code:

handles.timer = timer(...
    'ExecutionMode', 'fixedRate', ...   % Run timer repeatedly
    'Period', 1, ...                % Initial period is 1 sec.
    'TimerFcn', {@update_display,hObject}); % Specify callback

The opening function also initializes the slider Min, Max, and Value properties, and sets the slider label to display the value:

set(handles.periodsldr,'Min',0.01,'Max',2)
set(handles.periodsldr,'Value',get(handles.timer('Period'))
set(handles.slidervalue,'String',...
    num2str(get(handles.periodsldr,'Value')))

A call to surf renders the peaks data in the axes, adding the surfaceplot handle to the handles structure:

handles.surf = surf(handles.display,peaks);

Finally, a call to guidata saves the handles structure contents:

guidata(hObject,handles);

startbtn_Callback

startbtn_Callback calls timer start method if the timer is not already running:

if strcmp(get(handles.timer, 'Running'), 'off')
    start(handles.timer);
end

stopbtn_Callback

stopbtn_Callback calls the timer stop method if the timer is currently running:

if strcmp(get(handles.timer, 'Running'), 'on')
    stop(handles.timer);
end

periodsldr_Callback

periodsldr_Callback is called each time you move the slider. It sets the timer period to the slider current value after removing unwanted precision:

% Read the slider value
period = get(handles.periodsldr,'Value');
% Timers need the precision of periods to be greater than about
% 1 millisecond, so truncate the value returned by the slider
period = period - mod(period,.01);
% Set slider readout to show its value
set(handles.slidervalue,'String',num2str(period))
% If timer is on, stop it, reset the period, and start it again.
if strcmp(get(handles.timer, 'Running'), 'on')
    stop(handles.timer);
    set(handles.timer,'Period',period)
    start(handles.timer)
else               % If timer is stopped, reset its period only.
    set(handles.timer,'Period',period)
end

The slider callback must stop the timer to reset its period, because timer objects do not allow their periods to vary while they are running.

update_display

update_display is the callback for the timer object. It adds Gaussian noise to the ZData of the surface plot:

handles = guidata(hfigure);
Z = get(handles.surf,'ZData');
Z = Z + 0.1*randn(size(Z));
set(handles.surf,'ZData',Z);

Because update_display is not a GUIDE-generated callback, it does not include handles as one of its calling arguments. Instead, it accesses the handles structure by calling guidata. The callback gets the ZData of the surface plot from the handles.surf member of the structure. It modifies the Z matrix by adding noise using randn, and then resets the ZData of the surface plot with the modified data. It does not modify the handles structure.

figure1_CloseRequestFcn

MATLAB calls the figure1_CloseRequestFcn when you click the close box of the GUI. The callback cleans up the application before it exits, stopping and deleting the timer object and then deleting the figure window.

% Necessary to provide this function to prevent timer callback
% from causing an error after GUI code stops executing.
% Before exiting, if the timer is running, stop it.
if strcmp(get(handles.timer, 'Running'), 'on')
    stop(handles.timer);
end
% Destroy timer
delete(handles.timer)
% Destroy figure
delete(hObject);
Was this topic helpful?