You can configure a Loop to pass individual elements of an array or a collection into and out of the loop.
If you wire an array or a collection to a For Loop or While Loop, you can read and process every element in that array or collection by enabling auto-indexing. When you wire an array or collection from an upstream node to an auto-indexing input tunnel on the loop border and enable auto-indexing on the input tunnel, elements of that array or collection enter the loop one at a time, starting with the first element.
Complete the following steps to pass elements into an array through a loop:
Note To avoid default data output values when a loop does not execute, enable auto-indexing every time a For Loop has an output tunnel. |
A bracketed glyph appears on the loop output tunnel to indicate that auto-indexing is enabled.
Complete the following steps to configure the output tunnel of the loop to return the indexed value of an array, the last value of an array, or a concatenated array:
Refer to the following table for more information about each of the output options the tunnel can return.
Output Name | Description |
---|---|
Indexing | The output array receives a new element from every iteration of the loop. Therefore, auto-indexed output arrays are always equal in size to the number of iterations. For example, if the loop executes 10 times, the output array has 10 elements. |
Last value | The output tunnel returns only the element from the last iteration of the loop. |
Concatenating | LabVIEW appends all inputs in order, forming an output array of the same dimension as the array input wired. The Concatenating tunnel mode concatenates arrays the same as the Build Array function. |
Between the output tunnel and the next node, the thickness of the wire indicates whether the loop is indexing, returning the last value, or concatenating the values. Indexing creates a thicker wire than concatenating because the wire contains an array of higher dimension instead of an array of the same dimension. When the output tunnel returns the last value, the wire is thinner than when indexing or concatenating.
Note The loop indexes scalar elements from 1D arrays, 1D arrays from 2D arrays, and so on. The opposite occurs at output tunnels. Scalar elements accumulate sequentially into 1D arrays, 1D arrays accumulate into 2D arrays, and so on. |
Refer to the labview\examples\Arrays\Arrays.lvproj for examples of building arrays by disabling auto-indexing.
Open example Find related examples
Loop Type | Array or Collection Configuration | Auto-Indexing Behavior | Example | ||
---|---|---|---|---|---|
For Loop | One array or collection wired to an input tunnel and an unwired count terminal | If you enable auto-indexing on an array or a collection wired to a For Loop input tunnel, LabVIEW uses the array or collection size as the number of times to execute so you do not need to wire the count terminal. LabVIEW enables auto-indexing by default for every array or collection you wire to a For Loop.
| — | ||
For Loop | Wired count terminal | If you enable auto-indexing when the count terminal is wired, the actual number of iterations that LabVIEW executes is the smaller of either the array or collection size or the value of the count terminal. | For example, if you wire an array or a collection with 10 elements to a For Loop and a value of 15 to its count terminal, the loop executes only 10 times. | ||
For Loop | Multiple arrays or collections | If you enable auto-indexing for more than one tunnel, the number of iterations that LabVIEW executes is the smallest of the array or collection sizes. | For example, if two auto-indexed arrays enter the loop, with 10 and 20 elements respectively, and you wire a value of 15 to the count terminal, the loop executes only 10 times, indexing all elements of the first array but only the first 10 elements of the second array. As another example, if you plot data from two sources on one graph and you want to plot the first 100 elements, wire 100 to the count terminal. However, if the smaller data source includes only 50 elements, the loop executes 50 times and indexes only the first 50 elements of each data source. Use the Array Size function to determine the size of arrays. | ||
While Loop | One wired array or collection | If you enable auto-indexing for an array or collection entering a While Loop, the While Loop indexes the array or collection the same way as a For Loop. However, the number of iterations a While Loop executes is not limited by the size of the array or collection because the While Loop iterates until a specific condition occurs. If the number of loop iterations is greater than the array or collection size, the While Loop handles all the elements in the array or collection and uses the default value of the array or collection element type for later iterations.
You can prevent the default value from passing into the While Loop by using the Array Size or Collection Size functions, which indicate how many elements are in the array or collection. Configure the While Loop to stop executing when the number of iterations equals the array or collection size. Auto-indexing for While Loops is disabled by default.
| — |
Grouping Data with Collections
For Loops: Repeating Operations a Set Number of Times