How to stop a loop using a push button?
4 views (last 30 days)
Show older comments
I have created a GUI added axes with two push button.The first push button is for starting the function which takes values from Arduino and plots it and the second button is for stopping the graph which is currently being plotted. I also want that if I again push that first button then it will again start plotting.
I have done the plotting part but I am unable to do the stopping part.
2 Comments
Ameer Hamza
on 24 Apr 2018
How are you plotting the graph? Is there a callback function or loop which is running to plot the graph.
Answers (1)
Ameer Hamza
on 24 Apr 2018
In your code, you are running an infinite loop inside callback of push-button. Callback functions are intended to only perform a specific task, no to continue execution forever.
Here is a solution based on timer callback function inside App designer. You can start a timer which will update app.UIAxes after every a specific period of time (say 0.5 seconds). Now to start and stop the plotting, you can use UserData property present in every graphics object handle. This property is not used by MATLAB and only for the user to save extra information. You can use this property to convey a message between callbacks about whether you want to plot or not. The timer callback function will only plot if app.UIAxes.UserData = 1, otherwise stop plotting. The start push button callbacks will set app.UIAxes.UserData = 1 while stop pushbutton callback will set app.UIAxes.UserData = 0.
Mathwork does not allow attaching mlapp files, therefore, I have uploaded it here. You can look at Code View and get an idea of how to modify the code for your own app.
0 Comments
See Also
Categories
Find more on Specifying Target for Graphics Output 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!