Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 36 ยท Data Types and Arrays

Arrays and Auto-Indexing

Create and manipulate LabVIEW arrays: 1D and 2D shapes, Index Array, Build Array, Array Subset, Initialize Array, polymorphism and performance tips.

Measurements come in sets: a buffer of voltages, a table of results, a series of timestamps. LabVIEW stores them in arrays. This lesson covers array shapes, the key functions, auto-indexing and performance habits. Since LabVIEW cannot run on this site, the code examples are JavaScript stand-ins that model the same operations; they are not LabVIEW output.

What an array is

An array is an ordered collection of elements that all have the same type. You can make arrays of numbers, Booleans, strings, clusters, and even arrays of arrays (in LabVIEW, through clusters). An array has:

  • Dimensions: 1D (a list), 2D (a table with rows and columns), 3D and more.
  • Indexes starting at 0.
  • Size per dimension.

To create an array control on the front panel, place an Array shell from Controls > Modern > Array, Matrix & Cluster, then drag a control of the element type into it. The shell shows an index display on the left, and you resize it to show several elements.

Wires: a 1D array is a thicker wire than a scalar; a 2D array is thicker still. Both are the color of their element type (orange for DBL).

Creating arrays

  • Array constant with element constants inside.
  • Build Array appends elements, or concatenates arrays. Right-click it and pick Concatenate Inputs to join arrays end to end.
  • Initialize Array makes an array of a given size, all with one value. Use it to preallocate a buffer.
  • Auto-indexed output tunnels from loops, described below.
  • Array From Waveform and other conversions, such as Spreadsheet String To Array.
  • Reshape Array, Transpose 2D Array and Array Subset.

Essential functions

All live in Programming > Array.

Function What it does
Array Size Number of elements per dimension
Index Array Get an element (or a row or column of a 2D array)
Replace Array Subset Change elements in place
Insert Into Array Insert elements at an index
Delete From Array Remove elements
Array Subset Return part of an array (index, length)
Search 1D Array Find the index of a value, or -1
Sort 1D Array Sort ascending
Reverse 1D Array Reverse order
Rotate 1D Array Rotate elements
Array Max & Min Maximum, minimum and their indexes
Split 1D Array Divide at an index
Interleave and Decimate 1D Array Take every Nth element

Index Array with an out-of-range index returns the default value of the element type (0, empty string), with no error. That silent default is a common source of mysterious zeros. Check Array Size or use Search 1D Array results before trusting an index.

Auto-indexing in loops

When a wire carrying an array enters a For Loop, the tunnel by default auto-indexes: iteration i receives element i, and N is set to the array length. When a wire leaves the loop with auto-indexing, results collect into an array in iteration order. This is the standard way to transform arrays, element by element.

Combine the two: array in, process each element, array out. In a While Loop auto-indexing defaults to off, and the loop count is set by your stop condition. If both an input array and N are wired on a For Loop, the smaller wins.

For 2D arrays, nest two For Loops: the outer loop iterates rows, the inner loop iterates columns.

Array basics

Index from 0
One element type
Array Size Per dimension

Out-of-range index gives a default, not an error.

1 / 2

Polymorphism

Many arithmetic functions are polymorphic. Add a scalar to an array and every element changes. Add two arrays and you get element-wise addition, with the output length equal to the shorter input. Multiply, compare and most Boolean functions behave the same way. This saves loops: a scale-and-offset of a whole signal is one Multiply and one Add, with no loop at all.

Performance habits

  • Preallocate with Initialize Array and use Replace Array Subset instead of calling Build Array inside a loop. Appending forces LabVIEW to grow the array repeatedly and may copy it each time.
  • Prefer auto-indexed output tunnels to build arrays in loops: LabVIEW can allocate memory efficiently for them.
  • Avoid unneeded wire branches of huge arrays that are modified: they may trigger a copy. Use Show Buffer Allocations when profiling.
  • Use In Place Element Structure for advanced in-place modification.
  • Pass big arrays to subVIs through the wire as normal; LabVIEW passes a reference internally and copies only when needed.

This models index semantics. JavaScript returns undefined for out-of-range; the stand-in maps it to the default 0 to mimic LabVIEW.

JS Stand-in: Index Array, Search 1D Array, polymorphism

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Stand-in 2: auto-indexing and 2D arrays

A For Loop over a 2D array with two nested loops. Also compares appending (Build Array in a loop) with preallocating (Initialize Array plus Replace Array Subset) for correctness; both give the same result, but the second avoids repeated growth.

JS Stand-in: nested loops over a 2D array

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Index Array return for an index beyond the end of the array?

    It silently returns the default, such as 0, so check the size first.
  2. 2. When an array is wired into a For Loop with auto-indexing, what sets N?

    N is set automatically to the array size.
  3. 3. What is the best way to build a large array in a loop?

    Both avoid repeated growth and copying.
  4. 4. Adding two arrays of lengths 4 and 2 gives an array of what length?

    Polymorphic functions use the shorter input length.

Summary

  • Arrays hold elements of one type, indexed from 0, in 1D, 2D or more dimensions.
  • Learn Index Array, Build Array, Array Subset, Initialize Array, Search 1D Array and Sort.
  • Auto-indexing turns array wires into per-iteration elements, and results back into arrays.
  • Many functions are polymorphic and work on whole arrays without loops.
  • Preallocate or use auto-indexed outputs for large arrays.

Next: Clusters and Type Definitions