Workshop 2b: Intermediate LabVIEW
Goal of this workshop:
To learn how to use arrays, loops, and structures in LabVIEW.
1-D Arrays in LabVIEW
are displayed in one column (or row) in LabVIEW front panel.
- the array is re-sizable; you can change the number of elements
to display by clicking on the lower-right corner of the array.
- the index, located at the upper left, displays the index
of the first displayed element within the array. The index starts
counting at zero.
- in the diagram, 1-D array is represented by a thick wire.
- a 1-D array is analogous to a 1-D matrix.
2-D Arrays in LabVIEW
- 2-D arrays are displayed with two indices on the upper-left.
The array is re-sizable in horizontal and vertical direction.
- the first index corresponds to the number of the row, and
the second index indicates the number of the column.
- in the diagram, 2-D array is represented by a double wire.
- a 2-D array is analogous to a 2-D matrix.
Array Calculations in LabVIEW
- most simple arithmetic operations in LabVIEW can be used
with arrays without any problems. You can mix arrays and numbers
for inputs; however, data type for output must match the input
Loops in LabVIEW
Loop structures are used to repeat certain actions. In LabVIEW,
the actions to repeat are placed inside the loop structure, which
appear as a frame. You can re-size the frame to fit around all
the actions you want in it.
- While Loops (Functions > Structures > While
- While loops are used when you want some actions to repeat
until a condition is met.
- the green circular arrow in the loop takes in the boolean
condition, and the program stops when the condition turns to
FALSE (repeats while the condition is TRUE).
- the index of the loop is indicated by "i," and
it outputs the current iteration count.
- while loops are useful when using a switch: repeat a certain
action (or simply stall a program) until you press a button.
- Making a switch using a While Loop
- open a new program, and create a Boolean Control (a STOP
- make a digital indicator (output) in the front panel. Call
- in the diagram, create a While Loop that surrounds both the
switch and i. Go to Functions > Structures > While Loop,
and drag the loop around the two variables.
- "i" will be the counter; wire this into the blue
"i" icon in the loop.
- we want the program to keep on counting "i" until
we hit the Stop button. The condition in a While Loop (in this
case the status of the button) is wired into the icon with the
green circular arrow.
- recall, from Workshop 2a, that the default value of a boolean
variable is FALSE. Since the loop repeats while the condition
is TRUE, we need to negate the value. We can do this by inserting
a "not" function into the wire between the switch and
the green arrow (right-click on the wire, choose "insert,"
go to Boolean > Not.)
- run the program to see how the program works. Notice that
"i" gets very large quickly.
- A good way to slow down a loop is to time it using one of
the Time functions. Also, you can have the program display time,
instead of i, by timing the loop.
- in the diagram, go to Functions > Time & Dialog >
Wait (ms). This function takes in an input, in milliseconds,
and stalls the program until the specified time passes. For our
case, wire a numerical constant, set to "1000," into
- run the program again. This time, "i" should display
the time elapsed since you began the program, in seconds (each
iteration takes 1000 ms, or one second).
- Shift Registers
- a "shift register" in LabVIEW allows you to carry
values from one loop iteration to the next.
- to make a shift register, right-click on the frame of a while-loop
or a for-loop, and select "Add Shift Register." Two
boxes with arrows will appear; one on the left and one on the
right side of the frame.
- Using shift registers
- we will make the program display the sum of is as it
iterates the loop (in the first iteration it will display 0;
1 in the next iteration, 3 = 1+2 in the next, and so on).
- in the front panel, make a numeric indicator, "sum."
- in the diagram, right-click on the While loop frame to create
a shift register.
- shift registers must be initialized , which then sets its
data type. Initialization value is placed outside the loop, and
is wired into the left box. Initialize this shift register with
a numeric constant, 0.
- inside the for-loop, make an addition function. We will add
i to the previous shift register value, which comes out of the
- wire the output into "sum" to display the result.
- to assign a new value to the shift register (equal to "sum"),
wire this into the right box.
- run the program. If everything was done correctly, then "sum"
should display the sequence 0, 1, 3, 6, 10, 15,
- For Loops (Functions > Structures > For Loop)
- For loops are used to repeat actions for a known number of
- the index of the loop is indicated by "i," and
it outputs the current iteration count.
- Auto-indexing feature
- LabVIEW automatically creates 1-D arrays when values are
pulled out of an array.
- Similarly, when you wire a 1-D array into a For-loop, "N"
is automatically set to be the size of the array, and the elements
are taken one-by-one in the loop.
- Auto-indexing can be disabled (or enabled) by right-clicking
on the black box on the wire at the loop frame. Disabling the
feature makes the whole array (not just an element at i) go into
- 1-D array calculation with For Loops
- create an array
- in a new program, make an empty array (Controls > Array
& Cluster > Array). Call this "A."
- specify a type by dragging in a control or indicator into
the box. Make a numerical control (Controls > Numeric >
Digital Control) and place this inside the array.
- make another array with digital indicators, and call it "B."
- re-size both arrays so that 5 (or more) elements are visible.
- in the front panel, make a digital control (not an array),
and call this "C."
- create a for-loop and place between A, B, and C. A and C
should appear on the left, B on the right.
- create a division operator, and place it inside the for-loop.
- wire A, B, and C into the division operator, so that the
program performs A/C = B.
- if Auto-indexing is enabled, then the wire from A should
turn from thick to thin at the loop frame. Similarly, wire to
B should turn from thin to thick as it passes out of the loop.
If this is not the case, you should have a broken arrow (syntax
error). To correct this, enable auto-index for A and B.
- when there are no syntax errors, run the program. Does it
work as you had expected?
- More with auto-indexing
- in the front panel, create an empty array and call it "C."
Drag the previous "C" into this array to make it into
- go into the diagram and re-wire C (the array) into Divide.
Make sure that auto-index is enabled.
- enter values into C (as many as you have for A) and run the
program. This version of the program takes each element in A
and divides it by the corresponding value in C.
Array Functions in LabVIEW
There are many sub-vis that are specific to array operations.
Included are statistical functions such as maximum and minimum
values, average, and standard deviation, and array tools to re-shape
and pick out elements from arrays. See LabVIEW Reference Sheet
for more information.
- Using an array function
- use an array function to add all elements in an array. The
summation function is at Functions > Numeric > Add Array
- create an indicator in the front panel, and call it "E."
Back in the diagram, input B into the summation function, and
wire the output into E.
- run the program. E should display the sum of all the numbers
- Using an array function / Auto-indexing Part 2
- this time, we want to create an output array (B) that is
filled up with numbers from Array A, corresponding to indices
in array D (fill D with integers which would be the indices to
pull out of A).
- create a new array with numerical controls, and call it "D."
- in the diagram, create a for-loop. We want D to goes into
the loop with auto-indexing enabled, and into the bottom of the
"index array" function.
- on the top of the function, we will input A, with auto-indexing
disabled. The output goes into B (you will need to delete the
wire between the previous division and B).
- enter integers into D (being no larger than the size of array
A), and run the program.
Structures: Sequence and Case
Sequence structures allow you to execute certain commands in a
specific order. This is important in LabVIEW because items placed
on one window are executed in random order unless they are organized
in a sequence structure.
- Using sequence structures
- we will use the program created for while-loop exercise to
- suppose you wanted an LED light to show up after you hit
the stop button.
- first create an LED indicator in the front panel.
- in the diagram, place a sequence structure around the while-loop.
Make sure that the LED is outside of the sequence structure.
- right-click on the sequence structure, and choose "Add
Frame After." This creates Frame 1 (the first one was Frame
- drag the LED into Frame 1, and assign a boolean constant
to it (TRUE).
- you can switch between the different frames using the arrows
at the top of the sequence.
- run the program.
Case structures execute different commands based on the value
that goes into the condition. Case structures in LabVIEW accept
both integers and booleans as the condition; if a boolean is used,
then the structure is similar to If-statements in other programming
- Using case structures
- we will now make the program to output "5" when
i is equal to a specified number, and "1" for other
- in the front panel, create a numeric indicator, and call
this "y." Also create a numeric control, and input
an integer; call this "x."
- in the While-loop (you might need to expand both the sequence
and the while-loop to make more room), create a case statement.
- first, we need to test to see if i is equal to x. We will
use the "Equal?" operator to do this. This function
outputs TRUE if the two inputs are equal, and FALSE if otherwise.
- output from "Equal?" goes into the condition box
in the case structure (the green ? mark)
- in the case structure, create a numeric constant in each
TRUE and FALSE cases (switch between the two using the arrow
at top). Value for TRUE is 5, and for FALSE is 1.
- from the TRUE case, wire the "5" into y. Notice
that the "run" button has turned into a broken arrow.
Click on the button to see what is wrong.
- when outputting from a case structure, the value must be
assigned in each case. This time, you have an error because you
have not yet assigned a value to "y" from the FALSE
case. Wire the "1" into the small white rectangle on
Case frame to correct this.
- input a small integer in x, and run the program. Watch the
value in y and i carefully to see if the program works.
2 | Introduction
Reference | Image
Please send all corrections/questions/comments
to Haruna Tada at email@example.com