LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Execute a For Loop in a While Loop Only Once

Also don't forget the built-in templates in Create Project.  The State Machine template IS really simple, but it gets the point across and it's a good place to start from and modify to your needs.

NIquist_0-1675870348512.png

 

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019
0 Kudos
Message 11 of 23
(1,318 Views)

@TheWolfmansBrother wrote:

Move the for loop out of the while loop so that when "the user hits a button" the while loop stops, and you move on to running whatever is in the for loop (may not even need the for loop if it's only executing once), once that stuff executes the VI is complete. 


No!!!!

 

altenbach_0-1675871429678.png

 

 

It is completely unhelpful to post some half-baked and incomplete downhill code fragment that has little to do with the questions (there isn't even a FOR loop!). A proper code architecture (such as the state machine mentioned) will even allow the user to select a new set to be processed the same way later. Please also give your files a unique name before attaching. "Untilted 2.vi" is not a reasonable file name. My download folder already contains many of these, so it got renamed by the browser to "Untitled 2 (10).vi", potentially breaking dependencies and causing confusion.

0 Kudos
Message 12 of 23
(1,311 Views)

@adamdempsey wrote:

I have a for loop inside a while loop in a VI.  The while loop is there so the user can select some Boolean options on the front panel and navigate to some file directories.  Then once everything is setup, the user hits a button, which executes a for loop and "post processes" the tdms files in the directory the user selected.  My problem is, once the for loop is finished, it just starts over again and starts processing the files over again.


Sounds like you should be using an Event Structure in your loop.  That way your code can react to all of the inputs (if needed) and run the FOR loop only when the "run" button is pressed.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 13 of 23
(1,303 Views)

@crossrulz wrote:
Sounds like you should be using an Event Structure in your loop.  That way your code can react to all of the inputs (if needed) and run the FOR loop only when the "run" button is pressed.

Assuming that the processing of TDMS files is a lengthy process (is it?), it should be done in a separate loop in order to keep the event structure responsive. The UI loop should not do any heavy lifting.

0 Kudos
Message 14 of 23
(1,297 Views)

Yes, the processing of the TDMS files is a lengthy process.  Could you post a brief example of what you are suggesting with an event structure.  I understand you are saying that when the users presses the "Process" button, a event is triggered that executes the for loop.  What I am confused about is that when the for loop is finished, I want the entire VI to stop.  I am struggling how to have a signal that tells me the for loop is done. 

 

Can you post an example?

0 Kudos
Message 15 of 23
(1,276 Views)

@adamdempsey wrote:

What I am confused about is that when the for loop is finished, I want the entire VI to stop.  I am struggling how to have a signal that tells me the for loop is done. 


You would just wire a true to the stop condition from within the event that processes the files in a FOR loop. It is all in the dataflow! The event case cannot complete until everything is it has completed.  (NOT recommended!, see below)

 

This is not proper architecture! A program is ended by the user, not by itself. (MS word does not "stop" when you are done writing that resume, e.g. when you press "save", it would save the document and then kill itself!). A proper state machine is set to run when opened, and waits in an idle state for user input. It will wait for the command and then start processing the tdms files, giving you progress updates and even a chance to interrupt or start over with different settings if you notice that it does not do what you meant. At the end, it will tell you that all files have been processed and how long it took, giving you the option to close the program or process another set.

 

 

0 Kudos
Message 16 of 23
(1,270 Views)

Ok, that all makes sense, so you are suggesting I use a state machine approach.  I should just follow the template examples.  I have never done this before.  I will look into it, but do you have any examples of what you are suggesting that I can start from?

 

Thanks!

0 Kudos
Message 17 of 23
(1,246 Views)

As mentioned earlier in this post,

See LabVIEW Wiki "State Machine" article, "External Links" Section 

CLA, CTA
0 Kudos
Message 18 of 23
(1,227 Views)

Did you wire the constant "1" to the For Loop?  Why do you even need a loop?  I presume when the user "hits a button", you either use an Event Structure to "do something" or a Case Structure in which there is (only) the For Loop.  Simply remove the For Loop (if using a Case).

 

Egad, it would be so much easier to understand the question and provide an appropriate response if you showed some code, even my bête noire, a "picture of the relevant code".  So I'm "guessing" how you are doing this ...

 

Bob Schor

0 Kudos
Message 19 of 23
(1,186 Views)

I really don't see whats wrong with the method I suggested lol. OP wants to run what's in the while loop, then run what is in the for loop (once), and then end. Literally asking how to run one piece of code continuously until they hit a button which causes another piece of code to run then ends. And here you are telling them they need to restructure the whole thing. Going off just the first post, which I was, you would have no idea if this piece of code is a sub VI already in a state machine. I don't know about the code you write, but when I write code it usually consists of some bit of code that is run followed up by another piece of code. sometimes I run the first piece once and the second piece multiple times, or just like op I run the first piece for some iterations and when it's done or hit a button we're on to the second piece (I know it's crazy, but sometimes I even put both pieces in the same state of a state machine!). Of course OP can re-structure their whole program and use an event structure, a state machine, a QMH, (your absolutely right in that is the best way to do it) etc.. but you making those recommendations to someone who doesn't even know what they are or where to start isn't exactly solving their problem either. Sometimes people who don't have much experience or practice with programming (any language) just need a simple way to perform a simple task so they can move on with their day. Of course OP should go and learn more if they're going to be spending a lot of time working in LabVIEW (possibly see if they can get into Core 1 or something), but to just run one piece of code after another my, "half-baked and incomplete downhill code fragment", example would work just fine. 

 

 

Message 20 of 23
(1,178 Views)