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

4 Ansichten (letzte 30 Tage)
sebi
sebi am 21 Okt. 2020
Bearbeitet: Adam Danz am 23 Okt. 2020
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.

Akzeptierte Antwort

Adam Danz
Adam Danz am 21 Okt. 2020
Bearbeitet: Adam Danz am 23 Okt. 2020
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).

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by