MATLAB Creating Graphical User Interfaces

(Barry) #1
Automatically Refresh Plot in a GUIDE UI

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 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')
stop(handles.timer);

Free download pdf