From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Resetting a for loop counter and restarting the computation

Solved!
Go to solution

I have a while loop nested inside a for loop to do a calculation of a model. 

 

Lets say I am on the for loop counter 4. Suppose I wanted to keep track of the number of iterations on one the while loop, and if it exceeded a certain number say 100, exit the while loop.  I am doing this to avoid my model calculation being stuck at a spot due to optimization issues. I now want to restart the calculations of for loop counter 4. 

 

Is there a way to do this in labview? 

0 Kudos
Message 1 of 7
(4,399 Views)

Do your own counter using a shift register.

 

If you want more detailed help, please attach your code.

0 Kudos
Message 2 of 7
(4,391 Views)
Solution
Accepted by topic author AGovi

Well first of all, if you are going to restart the loop with the same program inside it after it got stuck is it not just going to get stuck again creating a permanent loop in the code? Would you not just want to move on and have some sort of identifier that one of the for-loops got stuck?

 

But I would recommend simply nesting your while-loop inside of another while loop, then connect the exit for the out while-loop to some variable from the inner while-loop, so if the optimization did not occur properly then it simply repeats the inner while-loop.

 

Loop-Breaker.JPG

Although I stick by my thoughts that you will just end up in a permanent loop if the optimization fails the first time.

0 Kudos
Message 3 of 7
(4,388 Views)

Thanks. I nested my while loop inside a while loop and that worked. 

0 Kudos
Message 4 of 7
(4,333 Views)

ogk.nz

 

My optimizer initializes randomly for each trial. So if I re-initialize the optimizer, the chances are that the optimizer started at a better spot and moved progressively towards a solution.

 

I used your idea of nesting the while loop inside another and this helped solve my problem!

 

Thank you so much!

0 Kudos
Message 5 of 7
(4,331 Views)

Since the max number of iterations is fixed and known before the loop starts, I would use an inner FOR loop, avoiding the constant checking of the iteration terminal. Show the conditional terminal to stop early.

0 Kudos
Message 6 of 7
(4,322 Views)

Altenbach,

 

Apologize for the delay. But thanks for the comment. I tried out the inner FOR loop method and works fine for me. 

 

 

0 Kudos
Message 7 of 7
(4,268 Views)