Lecture Notes

Download Report

Transcript Lecture Notes

CS320n –Visual Programming
Arrays
What We Will Do Today
• Learn about arrays and how to work with
them in LabVIEW
Visual Programming
Arrays in LabVIEW
2
What are Arrays?
• An array is like a list
• Up until now in LabVIEW we have dealt
with scalars, variables with a single value
• An array is a collection of elements
– all elements in the array have the same data
type: boolean, double, int, String
Visual Programming
Arrays in LabVIEW
3
Thinking About Arrays
Index
0
Element 2.3
1
2
3
4
5
1.7
3.9
0.2
-1.7
4.5
6
1.66
7
3.1
• Array elements are accessed by their
index
– Indices start at 0, not 1
• This is a one dimensional array
– arrays can have more dimensions
– 2 dimensional array, like a table of data
Visual Programming
Arrays in LabVIEW
4
Arrays in LabVIEW
• Arrays can be created on the
block diagram
– on block diagram select arrays
sub palette
– select array constant and drag
onto block diagram
– this creates an array shell
Visual Programming
Arrays in LabVIEW
5
Creating an Array on Block Diagram
• to finish creating the array
the data type the array holds
must be determined
• drag a constant from a sub
palette into the elements
portion
array shell
index
elements
– this determines the data type
of the array
after dragging numeric constant
into elements
Visual Programming
Arrays in LabVIEW
6
Array Constants on Block Diagram
• type the initial value into the
element section
• the array can be expanded
up and down or left and right
to view other elements
element 0 set to 12
– click and drag to expand array
to view more elements
Visual Programming
Arrays in LabVIEW
7
Array Constants on Block Diagram
Array expanded to show elements 0 through 10
Clicking on index array display other elements. The element on the far left (or
top) of the array has the index shown in the index box.
Light blue elements are elements that have not been initialized.
Visual Programming
Arrays in LabVIEW
8
Creating Arrays Via the Front Panel
• Arrays can also be
created via the front panel
• click on the Array and
Cluster sub palette
• click and drag an array to
the front panel
– this creates an array
control shell
Visual Programming
Arrays in LabVIEW
9
Creating Arrays Via the Front Panel
• a data type must be added to
the array shell
• drag a control or indicator of
the data type you want into
the shell
array shell
– can change from double to int
by right clicking on elements
and selecting “data range”
– click on icon under
“representation” and change to
an integer type
Visual Programming
Arrays in LabVIEW
10
Arrays on the Front Panel
• Control can be expanded to show more
elements of the array
• grey elements indicate uninitialized values
• the size (or length) of the array above is 1
• if other elements are given values, the
array size automatically expands
Visual Programming
Arrays in LabVIEW
11
Auto indexing
• LabVIEW has a feature with loops and
arrays called auto indexing
• If you wish to carry out an operation on
every element of an array a loop is
required
• assume we have an array and we want to
double every element
Visual Programming
Arrays in LabVIEW
12
Using Auto indexing
array indicator
Visual Programming
Arrays in LabVIEW
13
Notes on Previous slide
• no value wired to count terminal
– count based on length of array
• thick wire going in and coming out, but thin
inside the loop
– working with a single element of the array
• must create array indicator to show array
when finished
– can be a bit tricky
Visual Programming
Arrays in LabVIEW
14
Manipulating Arrays
• Many functions for working
with arrays
– initialize array
initial value
for all elements
size of the array
Visual Programming
Arrays in LabVIEW
15
Getting the Length of an Array
array input
Visual Programming
output is size
of array
Arrays in LabVIEW
16
Changing Values of Elements
• Giving an array, set the value at position 2
(actually the 3rd element of the array) to 1
more than its previous value
• Uses Index Array and Replace Array
Subset
resulting
array
value at element
array
array
index
index
Index Array
Visual Programming
new value for
element
Arrays in LabVIEW
Replace Array
Subset
17
Completed Diagram
Index Array (access element)
Visual Programming
Replace Array Subset
(Change value of 1 element)
Arrays in LabVIEW
18
Class work
• Download the ArrayExercise.vi program
from the class web site.
• The program creates an array of size
1000. The elements of the array are
random values from 1 to 100
• Add 1 to all elements that are odd.
• Use a case statement or select function
inside the loop to increment the array
element if R is equal to 1
Visual Programming
Arrays in LabVIEW
19