How can I stop manually a true while loop?

조회 수: 4(최근 30일)
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).

채택된 답변

Walter Roberson
Walter Roberson 2022년 11월 20일
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

추가 답변(1개)

the cyclist
the cyclist 2022년 11월 20일
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.

범주

Find more on Loops and Conditional Statements in Help Center and File Exchange

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by