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, seeGUIDE Migration Strategiesfor information on how to help maintain compatibility of the app with future MATLAB releases. To create new apps interactively,Develop Apps Using App Designerinstead.
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 app. Move the slider to set the refresh interval between 0.01 and 2.0 seconds. Then click theStart Randomizingbutton to start adding random noise to the plotted function. Click theStop Randomizingbutton to stop adding noise and refreshing the plot.
我n GUIDE, click theEditorbuttonto view the code.
Near the top of the Editor window, use theGo Tobutton to navigate to the functions discussed below.
ex_guide_timergui_OpeningFcn
Theex_guide_timergui_OpeningFcn
function executes when the app opens and starts running. This command creates thetimer
object and stores it in the处理
structure.
处理.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
Theupdate_display
function executes when the specifiedtimer
period elapses. The function gets the values in theZData
property of theSurface
object and adds random noise to it. Then it updates the plot.
处理= guidata(hfigure); Z = get(handles.surf,'ZData'); Z = Z + 0.1*randn(size(Z)); set(handles.surf,'ZData',Z);
periodsldr_Callback
Theperiodsldr_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 thetimer
object.
% Read the slider valueperiod = 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))%如果计时器,停止它,重置时期和年代tart it again.ifstrcmp(get(handles.timer,'Running'),'on')停止(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
Thestartbtn_Callback
function calls thestart
method of thetimer
object if the timer is not already running.
ifstrcmp(get(handles.timer,'Running'),'off') start(handles.timer);end
stopbtn_Callback
Thestopbtn_Callback
function calls thestop
method of thetimer
object if the timer is currently running.
ifstrcmp(get(handles.timer,'Running'),'on')停止(handles.timer);end
figure1_CloseRequestFcn
Thefigure1_CloseRequestFcn
callback executes when the user closes the app. The function stops thetimer
object if it is running, deletes thetimer
object, and then deletes the figure window.
ifstrcmp(get(handles.timer,'Running'),'on')停止(handles.timer);end% Destroy timerdelete(handles.timer)% Destroy figuredelete(hObject);