Automatically Refresh Plot in a GUIDE App
Note
The GUIDE environment will be removed in a future release. After GUIDE is removed, existing GUIDE apps will continue to run in MATLAB® but they will not be editable in GUIDE.
To continue editing an existing GUIDE app, see GUIDE Migration Strategies for information on how to help maintain compatibility of the app with future MATLAB releases. To create new apps interactively, Develop Apps Using App Designer instead.
This example shows how to examine and run a prebuilt GUIDE app. The app displays a surface plot, adds random noise to the surface, and refreshes the plot at regular intervals. The app contains two buttons: one that starts adding random noise to the plot, and another that stops adding noise. The slider below the plot allows the user to set the refresh period between 0.01 and 2 seconds.
Open and Run the Example
Open and run the app. Move the slider to set the refresh interval between 0.01 and 2.0 seconds. Then click the Start Randomizing button to start adding random noise to the plotted function. Click the Stop Randomizing button to stop adding noise and refreshing the plot.
Examine the Code
In GUIDE, click the Editor button to view the code.
Near the top of the Editor window, use the Go To button to navigate to the functions discussed below.
ex_guide_timergui_OpeningFcn
The ex_guide_timergui_OpeningFcn
function executes when the
app opens and starts running. This command creates the timer
object and stores it in the handles
structure.
handles.timer = timer(... 'ExecutionMode', 'fixedRate', ... % Run timer repeatedly. 'Period', 1, ... % Initial period is 1 sec. 'TimerFcn', {@update_display,hObject}); % Specify callback function.
update_display
, which is
defined as a local function.update_display
The update_display
function executes when the specified
timer
period elapses. The function gets
the values in the ZData
property of the Surface
object and adds random noise to it. Then it
updates the
plot.
handles = guidata(hfigure); Z = get(handles.surf,'ZData'); Z = Z + 0.1*randn(size(Z)); set(handles.surf,'ZData',Z);
periodsldr_Callback
The periodsldr_Callback
function executes when the user
moves the slider. It calculates the timer period by getting the slider value and
truncating it. Then it updates the label below the slider and updates the period
of the timer
object.
% Read the slider value period = get(handles.periodsldr,'Value'); % 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. set(handles.timer,'Period',period) end
startbtn_Callback
The startbtn_Callback
function calls the
start
method of the timer
object if the timer is not already
running.
if strcmp(get(handles.timer, 'Running'), 'off') start(handles.timer); end
stopbtn_Callback
The stopbtn_Callback
function calls the
stop
method of the timer
object if the timer is currently
running.
if strcmp(get(handles.timer, 'Running'), 'on') stop(handles.timer); end
figure1_CloseRequestFcn
The figure1_CloseRequestFcn
callback executes when the user
closes the app. The function stops the timer
object if it is running, deletes the timer
object, and then deletes the figure
window.
if strcmp(get(handles.timer, 'Running'), 'on') stop(handles.timer); end % Destroy timer delete(handles.timer) % Destroy figure delete(hObject);