Showing results for 
Search instead for 
Did you mean: 

Iterating a For Loop on an array index, and repeating

I have a seemingly simple task, but I am new to LabView and visual programming:

I want to write a loop that iterates for each element in an array, and then starts over again.  It will feed into a case structure.  In textual programming, it would look something like this:

For each "element(i=1:3)" in "array(1DX3E)";
If "element(i)" < n, multiply by a;
If "element(i)" <m and >n, multiply by b;
If "element(i)" >m, multiply by c
end "for"

While "Start" = true, repeat for loop

From a beginner's perspective, the for loop only seems iterable on a constant, for instance I could say repeat 3 times, but this is not indexing to the unique element of the array.  Is there an elegant way to do this in LV 8 without going from an array to a cluster, un-bundling it, and then writing 3 different case structures????????  This is what I have now and it is rather obtuse!

Thanks for any input,
0 Kudos
Message 1 of 6
The number of times a for loop iterates can be determined either by wiring a value (constant or calculated) to the N terminal or by wiring auto-indexing arrays into the loop. Normally, if you wire an array into the loop (it has to come from outside) it will be automatically auto-indexing and you will have to disable it if you don't want it to be. This should give you exactly what you want. Note that if you have several arrays, the loop will iterate only to the size of the smallest array.
To learn more about LabVIEW, I suggest you try searching this site and google for LabVIEW tutorials. Here and here are a couple you can start with and here are some tutorial videos. You can also contact your local NI office and join one of their courses.
In addition, I suggest you read the LabVIEW style guide and the LabVIEW user manual (Help>>Search the LabVIEW Bookshelf).

Try to take over the world!
Message 2 of 6
Here is a literal translation of your code. 😄
A inner FOR loop with autoindexing and an outer while loop with a shift register is all you need (I assume you want to substitute the new values for the next iteration)..
Let me know if you have questions.
Message 3 of 6
Thank you both for your help.  My problem seems fixed, and it wasn't as hard as I had convinced myself it was going to be. 

I do have a technical question, however.  I implemented a loop quite like the example posted herein (the actual problem was much more complicated, but the basic principle worked).  In the beginning I used the timed while loop along with the self-indexing array.  I originally set it to read every 1000 ms.  Then after 5 minutes of monitoring, the program stopped with an error stating something like "data were available and now they aren't, try using a faster read rate...."  So I basically got rid of the timed loop, and now it is simply a while loop with no time signature.  The VI ran all night long without a hitch.  Can anyone explain why this would happen?  It is not intuitive to me, unless the 1000 ms read rate was much slower and out of sync with the send rate of the PXI chassis....

Thanks again for the input...

0 Kudos
Message 4 of 6


I believe that your assessment of why your timed looped was erroring is correct. The only reason that it would say that data it there is no longer available is that the buffer was overwritten. I am guessing that you are using this as part of some post processing on data that you are acquiring. If this is so would you let me know what speed your acquistion is acquiring at and how many samples you are trying to process at once. Thanks

Tyler Heikes

Applications Engineer

National Instruments

0 Kudos
Message 5 of 6
Thanks for the input Tyler.

I am currently running the VI without any timer in the while loop, so I assume that the speed at which data are coming in is determined by the 6224 that is reading the data or the 8336 that is talking to the computer.  I don't need to read data super fast, which is why I originally tried a timed loop with 1000 ms wait period.  I am just taking in voltages from various pressure gauges and post-processing that to an actual pressure reading (two sub-VI's for two different types of gauges).  It is a monitoring system, so one second refreshing would be fine with me.  If a timed loop won't work, I assume I'll see a small slow-down as I begin to occupy more channels on the PXI chassis.

0 Kudos
Message 6 of 6