Passing Elements in an Array through a Loop

LabVIEW 2013 Help

Edition Date: June 2013

Part Number: 371361K-01

»View Product Info

If you wire an array to a For Loop or While Loop, you can read and process every element in that array by enabling auto-indexing. When you wire an array from an external node to an input tunnel on the loop border and enable auto-indexing on the input tunnel, elements of that array enter the loop one at a time, starting with the first element.

Complete the following steps to pass elements in an array through a loop.

  1. Right-click the tunnel at the loop border.
  2. Select Disable Indexing from the shortcut menu to disable auto-indexing, which passes the elements in the array and returns an array with the same number of elements.

    Select Enable Indexing from the shortcut menu to enable auto-indexing, which passes the elements in the array but returns one element.
    Note 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.

You can configure the output tunnel of the loop to return the last value, the indexed value, or a concatenated array by right-clicking the output tunnel and selecting Tunnel Mode»Last Value, Indexing, or Concatenating from the shortcut menu respectively. If you select 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. If you select Last Value, only the element from the last iteration of the loop passes to the next node on the block diagram. If you select 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.

A bracketed glyph appears on the loop output tunnel to indicate that auto-indexing is enabled. Between the output tunnel and the next node, the thickness of the wire indicates whether the loop is indexing 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.

Note 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.

Automatically Setting the For Loop Count using Auto-Indexing

If you enable auto-indexing on an array wired to a For Loop input terminal, LabVIEW sets the count terminal to the array size so you do not need to wire the count terminal. Because you can use For Loops to process arrays one element at a time, LabVIEW enables auto-indexing by default for every array you wire to a For Loop. You can disable auto-indexing if you do not need to process arrays one element at a time.

If you enable auto-indexing for more than one tunnel or if you wire the count terminal, the actual number of iterations becomes the smaller of the choices. 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 still only executes 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.

Auto-Indexing While Loops

If you enable auto-indexing for an array entering a While Loop, the While Loop indexes the array 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 because the While Loop iterates until a specific condition occurs. When a While Loop indexes past the end of the input array, the default value for the array element type passes into the loop. You can prevent the default value from passing into the While Loop by using the Array Size function. The Array Size function indicates how many elements are in the array. Set up the While Loop to stop executing when the number of iterations equals the array size.

Auto-indexing for While Loops is disabled by default.

Caution  Because you cannot determine the size of an output array in advance, enabling auto-indexing for the output of a For Loop is more efficient than that of a While Loop. Iterating too many times can cause your system to run out of memory.


Not Helpful