How can I stop manually a true while loop?

3 Ansichten (letzte 30 Tage)
Eduardo Gabriel González Blancas
Beantwortet: Walter Roberson am 20 Nov. 2022
I'm using the a true While loop to get some data from arduino because I can leave it there collecting information without a specific time or iterations, but I want to stop it manually when I decide it's enough (I tried to use a Ctrl + C, but that clear all the data I mean my function doesn't return any value).

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Nov. 2022
Pressing control-C will interrupt a calculation, and there is no way to catch control-C to handle it smoothly.
  • you could, each iteration, store variables in a data structure or file, along with an indication of how much of the data structure has been completely written to, and then after you control-C you could execute a function to extract the most recently complete information out of what is saved
  • you could create a toggle button or other kind of control, whose callback destroys the control. Then inside your loop, each iteration you can check whether the control still exists, and if so then leave the loop.
  • you could create a toggle button uitogglebutton just before your loop. Then inside your loop, each iteration you can check to see whether the button has been clicked, and if so then leave the loop. If you do this it is a good idea to first check whether the control still exists, in case the user x'd to close the control instead of just clicking the button
  • you could create a KeyPressFcn callback for a figure, and have the callback change some setting that you can test inside the loop

Weitere Antworten (1)

the cyclist
the cyclist am 20 Nov. 2022
When a file is executing, the Run button in the editor window becomes a Pause button. Pressing that will pause execution, with access to the variables.

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by