Main Content

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, 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 Example

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.

Examine the Code

  1. 我n GUIDE, click theEditorbuttonto view the code.

  2. Near the top of the Editor window, use theGo Tobutton to navigate to the functions discussed below.

ex_guide_timergui_OpeningFcn

Theex_guide_timergui_OpeningFcnfunction executes when the app opens and starts running. This command creates thetimerobject 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.
The callback function for the timer isupdate_display, which is defined as a local function.

update_display

Theupdate_displayfunction executes when the specifiedtimerperiod elapses. The function gets the values in theZDataproperty of theSurfaceobject 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_Callbackfunction 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 thetimerobject.

% 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_Callbackfunction calls thestartmethod of thetimerobject if the timer is not already running.

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

stopbtn_Callback

Thestopbtn_Callbackfunction calls thestopmethod of thetimerobject if the timer is currently running.

ifstrcmp(get(handles.timer,'Running'),'on')停止(handles.timer);end

figure1_CloseRequestFcn

Thefigure1_CloseRequestFcncallback executes when the user closes the app. The function stops thetimerobject if it is running, deletes thetimerobject, and then deletes the figure window.

ifstrcmp(get(handles.timer,'Running'),'on')停止(handles.timer);end% Destroy timerdelete(handles.timer)% Destroy figuredelete(hObject);

Related Topics