How to stop a loop using a push button?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Ameer Hamza
2018-4-24
How are you plotting the graph? Is there a callback function or loop which is running to plot the graph.
回答(1 个)
Ameer Hamza
2018-4-24
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!