How to stop for-loop outside of the loop?

7 ビュー (過去 30 日間)
Mark Golberg
Mark Golberg 2019 年 10 月 8 日
編集済み: Daniel M 2019 年 10 月 9 日
Hello,
I have the below simple code.
It displays a counter of seconds.
How can I stop it with a command? NOT by pressing Ctrl+C?
I am not asking about the break command. Sometimes I want it to finish to count all the seconds, and sometimes I want to terminate it in the middle. For now I'm using Ctrl+C. I was wondering if there is a more elegant way.
THANK YOU!
for t = 1:timeDuration
disp(['Elapsed time ' num2str(t) ' seconds']);
pause(1);
end
  1 件のコメント
Stephen23
Stephen23 2019 年 10 月 9 日
"How can I stop it with a command? NOT by pressing Ctrl+C?"
"I was wondering if there is a more elegant way"
What you are requesting requires asynchronous code evaluation, which requires something like a timer object or a GUI or a suitable class. Which would you prefer?

サインインしてコメントする。

採用された回答

Daniel M
Daniel M 2019 年 10 月 8 日
Sounds like you want a GUI of a stop watch.
https://www.mathworks.com/matlabcentral/fileexchange/12628-stopwatch
  6 件のコメント
Mark Golberg
Mark Golberg 2019 年 10 月 9 日
Yes, but I’m asking how can I start the stopwatch with a command line.
stopwatch only opens the GUI. How can I make the clock start ticking with a command, NOT by pressing any key.
Daniel M
Daniel M 2019 年 10 月 9 日
編集済み: Daniel M 2019 年 10 月 9 日
Read the documentation. If you want to start the GUI with the time already running, type stopwatch(clock).
If you want to start and stop the timer using keyboard commands, use the commands listed. You said you are currently using Ctrl+C, which means that you are clearing typing into the command window while your script is running.
If you want programmatic access to the functions inside the GUI, it's probably easier to just learn to use timer objects instead.
If you do NOT want to have to type into the command window, then you must have a programmatic way of knowing when to start and stop the timer. In this case, forget the GUI, program those conditions and use tic toc.
You're not being very precise in your language in what you want. If you want help, you need to properly phrase your issue.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by