190 likes | 204 Views
CS320n –Visual Programming. Arrays. What We Will Do Today. Learn about arrays and how to work with them in LabVIEW. 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
E N D
CS320n –Visual Programming Arrays
What We Will Do Today • Learn about arrays and how to work with them in LabVIEW Arrays in LabVIEW
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 Arrays in LabVIEW
Thinking About Arrays • 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 Index 0 1 2 3 4 5 6 7 Element 2.3 1.7 3.9 0.2 -1.7 4.5 1.66 3.1 Arrays in LabVIEW
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 Arrays in LabVIEW
Creating an Array on Block Diagram array shell • 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 • this determines the data type of the array index elements after dragging numeric constant into elements Arrays in LabVIEW
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 • click and drag to expand array to view more elements element 0 set to 12 Arrays in LabVIEW
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. Arrays in LabVIEW
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 Arrays in LabVIEW
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 • 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 array shell Arrays in LabVIEW
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 Arrays in LabVIEW
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 Arrays in LabVIEW
Using Auto indexing array indicator Arrays in LabVIEW
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 Arrays in LabVIEW
Manipulating Arrays • Many functions for working with arrays • initialize array initial valuefor all elements size of the array Arrays in LabVIEW
Getting the Length of an Array output is size of array array input Arrays in LabVIEW
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 resultingarray value at element array array index new value forelement Index Array index Replace ArraySubset Arrays in LabVIEW
Completed Diagram Index Array (access element) Replace Array Subset(Change value of 1 element) Arrays in LabVIEW
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 Arrays in LabVIEW