Processing Individual Elements in an Array with a Loop

FlexRIO Help

Edition Date: November 2015

Part Number: 372614J-01

»View Product Info

»Download the Help in ZIP Format

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 into an array through a loop:

  1. Right-click the tunnel at the loop border.
  2. Choose the option that corresponds to how you want to process array elements:
    • Select Disable Indexing from the shortcut menu to disable auto-indexing, which passes the entire array into the loop and returns an array with the same number of elements.
    • Select Enable Indexing from the shortcut menu to enable auto-indexing, which passes each individual element of the array into the loop and returns a single 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.

A bracketed glyph appears on the loop output tunnel to indicate that auto-indexing is enabled.

Configuring the Output Tunnel to Return an Alternative Value

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:

  1. Right-click the output tunnel.
  2. Select Tunnel Mode»Last Value, Indexing, or Concatenating from the shortcut menu respectively.

Refer to the following table for more information about each of the output options the tunnel can return.

Output NameDescription
IndexingThe 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 valueThe output tunnel returns only the element from the last iteration of the loop.
ConcatenatingLabVIEW 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 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.

Choosing a Type of Auto-Indexing for an Array Wired to a Loop

Loop TypeArray ConfigurationExplanationExample
For LoopOne array wired to input tunnel and an unwired count terminalIf you enable auto-indexing on an array wired to a For Loop input tunnel, LabVIEW uses the array 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 you wire to a For Loop.
Note Note  You can disable auto-indexing if you do not need to process arrays one element at a time.
For LoopWired count terminalIf you enable auto-indexing for this scenario, the actual number of iterations becomes the smaller of the choices.For example, if you wire an array with 10 elements to a For Loop and a value of 15 to its count terminal, the loop only executes 10 times.
For LoopMultiple ArraysLike the scenario above, if you enable auto-indexing for more than one tunnel, the number of iterations becomes the smaller of the array 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.
While LoopOne wired arrayIf 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.
Tip  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.

Related Information

For Loop

While Loop

Control and Indicator Data Types

Automatically Concatenating Arrays Leaving Loops

Build Array Function


Not Helpful