Is there an example of a GUI with a constantly-updating figure?
19 views (last 30 days)
Show older comments
MathWorks Support Team
on 21 Jan 2010
Edited: MathWorks Support Team
on 27 Jul 2020
I would like my GUI to include a constantly-updating figure, but am not sure how to go about this.
Accepted Answer
MathWorks Support Team
on 27 Jul 2020
Edited: MathWorks Support Team
on 27 Jul 2020
The attached GUIs demonstrate two ways of going about this. To run the GUIs you can execute the MATLAB files with the ".m" extension.
(Note: This references an older version of MATLAB that uses GUIDE, which is no longer recommended and will be removed in a future release. We now suggest using MATLAB App Designer to build GUIs.)
Both GUIs will, upon pressing the "Start" button, repeatedly plot a random line in the axes. Pressing the "Stop" button stops this process. The first GUI updates the plot as fast as possible. The second GUI updates the plot at a fixed, slower, rate.
Case 1: updating_example1
This GUI uses indefinitely-executing loop in the "Start" button's callback to implement the plotting. Each time the body of the loop is executed, a new line is plotted in the axes.
Breaking this loop is accomplished via a flag in the "handles" structure, which is set by an interrupting "Stop" callback. A DRAWNOW in the loop, and appropriately set "Interruptible" property, is necessary to allow this interruption, and allow the drawing events to be processed.
Case 2: updating_example2
This GUI uses a TIMER object, whose "TimerFcn" callback implements the plotting. The "Start" and "Stop" buttons start and stop the timer.
In both GUIs, there are a few special details that need to be addressed:
1) The figure's GUI-data may become out-of-sync with the local "handles" variable, so the GUIDATA function needs to be strategically used to put them back in sync.
2) One must handle the possibility of the "Start" or "Stop" button be pressed twice in a row.
3) One must handle the possibility that the figure is closed while the update loop or timer is still running.
Each GUI's program file has comments to explain how these issues are dealt with in each case.
1 Comment
Walter Roberson
on 10 Jul 2020
It looks to me as if likely the code framework is the same as is posted at https://www.mathworks.com/matlabcentral/answers/92157-figure-gui
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!