Does Ctrl+C interrupt after executing all commands in a line?

Dear community,
I am running a loop where some state variables are updated.
Sometimes I want to interrupt that loop using Ctrl+C, but I want to make sure that all my state variables have been updated for consistency.
Currently I have created a structure that embeds all my state variables that I update writting:
state = state_past;
The intent is to update all the structure content at once, making it unafectable by a Ctrl+C interruption.
Q1: Do you think updating a whole structure is not interrupted by Ctrl+C?
Rather than using this state structure approach, I was thinking to write all my updates on the same line this way:
t = t_past; x = x_past; y = y_past; % and so on...
Q2: Do you think Ctrl+C can not interrupt in the middle of this line?
Q3: Is there a way to make sure a complete bloc of code is executed together not being interrupted by Ctrl+C?
Thank you for your enlightments.

 채택된 답변

Adam Danz
Adam Danz 2020년 10월 21일
편집: Adam Danz 2020년 10월 23일

0 개 추천

Q1 & Q2 & Q3: No.
If ctrl+c is pressed before that line is executed or perhaps even while that line is executed it will be interrupted. If ctrl+c is executed afterwards the function will not return outputs. https://www.mathworks.com/help/matlab/matlab_env/stop-execution.html
Besides, this is a horrible design strategy. If you want the user to stop execution you can build in some flag-checks that check the status of a flag to decide whether to continue or not. The flag can be a property value such as a state button or something the user can control while the code is running. If the flag is false, for example, the functions can all terminate ('return'). Here's an example.
But see this disucssion, first (it's relevant whether you're using app designer or not).

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2020년 10월 21일

편집:

2020년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by