Clear Filters
Clear Filters

How to stop a loop using a push button?

4 views (last 30 days)
DEVESH DANGI
DEVESH DANGI on 24 Apr 2018
Answered: Ameer Hamza on 24 Apr 2018
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
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.
DEVESH DANGI
DEVESH DANGI on 24 Apr 2018

Yes, I have used two axes but it wont matter. I only wanted to know that how to stop the graphs being plotted.

Sign in to comment.

Answers (1)

Ameer Hamza
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.

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!