This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Automatically Refresh Plot in a GUIDE UI

About the Example

This example shows how to refresh a display by incorporating a timer in a UI 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 UI then analyzes and displays.

The UI 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 UI when you run it. The left figure shows the initial state of the UI. The right figure shows the UI 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.

The Timer Implementation

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


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:
A call to surf renders the peaks data in the axes, adding the surfaceplot handle to the handles structure: = surf(handles.display,peaks);
Finally, a call to guidata saves the handles structure contents:


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

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


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

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


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
% If timer is on, stop it, reset the period, and start it again.
if strcmp(get(handles.timer, 'Running'), 'on')
else               % If timer is stopped, reset its period only.
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 is the callback for the timer object. It adds Gaussian noise to the ZData of the surface plot:

handles = guidata(hfigure);
Z = get(,'ZData');
Z = Z + 0.1*randn(size(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 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.


MATLAB calls the figure1_CloseRequestFcn when you click the close box on the UI window. 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 code stops executing.
% Before exiting, if the timer is running, stop it.
if strcmp(get(handles.timer, 'Running'), 'on')
% Destroy timer
% Destroy figure

Related Examples

More About

Was this topic helpful?