Borrar filtros
Borrar filtros

Can I exit my while loop manually and let the code finish the script after the while loop?

1 visualización (últimos 30 días)
Hello,
I have some code where I'm in a never ending while loop which I am able to stop manually by pressing ctrl+c of by deleting the figure that opens during the loop. After the loop is some code that opens another figure and plots some values which were calculated in the while loop.
Is there a way where I have the possibility of ending the while loop on my command and still having Matlab finishing the rest of the script after the while loop? It doesn't matter in which part of the while loop it is executed/left.
I'm using Matlab R2015b.
My code looks something like this:
open a figure
while true
Calculate all sorts of magic
drawnow % Apply the newly calculated values on the earlier opened figure
pause(1)
end
open another figure and plot certain values from the loop

Respuestas (1)

KSSV
KSSV el 6 de Oct. de 2016
If you are not specific of where while has to exit, then why to use while in the first place?
To exit while loop, you may put some condition and exit the loop. Eg:
count = 0 ;
while count <100
count = count+1 ;
end
  1 comentario
Lauran Leermakers
Lauran Leermakers el 6 de Oct. de 2016
I'm rather new with Matlab so I can't really answer your question. The reason I'm using a while loop is because it is never ending, i think. Every iteration it calculates something and at some point i've seen enough and like to exit the while loop and plot some results.
When I would like to exit the loop is something which varies every time. So I rather not decide on beforehand when it will exit the loop.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by