You are on page 1of 67

Lesson 5 Arrays, Graphs, and Clusters

You Will Learn: A. About arrays


B. About generating arrays with loops C. Some basic array functions D. What polymorphism is E. Using graphs to display data F. About clusters G. Some cluster functions
LV Basics I 1

Arrays
Collection of data elements that are of same type

One or more dimensions, up to 2 31 elements per dimension


Elements accessed by their index First element is index 0
index 10-element array 2D array 0 1 2 3 4 Five-row by seven column array of 35 elements
LV Basics I 2

0 0

1 1

2 2

3 3

4 4

5 5

6 6

1.2 3.2 8.2 8.0 4.8 5.1 6.0 1.0 2.5 1.7

Array Controls and Indicators


1. Select the Array Shell from the Controls palette 2. Place data object inside shell

Add Dimension for 2D arrays

LV Basics I 3

Creating Array Constants


1. Select Array Constant shell from the Array subpalette

2. Place the data object in the array shell

LV Basics I 4

Creating and Using Arrays


Auto-Indexing loops accumulate arrays at their boundaries
For Loops auto index by default; While Loops do not

LV Basics I 5

Creating 2D Arrays

Inner loop creates column elements Outer loop stacks them into rows
LV Basics I 6

Common Array Functions

Array Size

Initialize Array

Array Subset
LV Basics I 7

The Build Array Function

Appending an element

Building a higher dimension array

Concatenate Inputs (default)


LV Basics I 8

The Index Array Function

Extracting an Element

Extracting a Row
LV Basics I 9

Polymorphism
Function inputs can be of different types All LabVIEW arithmetic functions are polymorphic
Combination Scalar + Scalar Array + Scalar Array + Array Array + Array
5 2 1 4 2 2 3 2 5 1 4 2 1 4 2 3 2 5 7 4 6 7 7

Result Scalar Array Array Array

3 6 4

4 6 7

Run Polymorphism Example.vi

LV Basics I 10

Exercise 5-1 on page 5-11


Students build Array Exercise.vi
Time to complete : 20 min.

LV Basics I 11

Graphs
Selected from the Graph subpalette Waveform Graph Plot an array of numbers against their indices XY Graph Plot one array against another
Plot Legend
(point and line styles)

Scale Legend
LV Basics I 12

Graph Palette

Single-Plot Waveform Graphs

Uniform X axis - initial X = 0.0, delta X = 1.0

Uniform X axis - you specify point spacing


LV Basics I 13

Multiple-Plot Waveform Graphs

Each row is a separate plot (initial X = 0, delta X = 1)

Each row is a separate plot (Bundle specifies point spacing of the X axis)
LV Basics I 14

XY Graphs
Non-uniform X axis Separate X and Y arrays define data points

LV Basics I 15

Exercise 5-2 on page 5-16


Students build Graph Waveform Array.vi
Time to complete : 30 min.

LV Basics I 16

Exercise 5-3 on page 5-22


Students build Temperature Analysis.vi *This VI will be used in a later exercise.
Time to complete: 25-30 min.

LV Basics I 17

Exercise 5-4 on page 5-26 (Optional)


Students build Graph Circle.vi
Time to complete : 20 min.

LV Basics I 18

Chart and Graph Use Summary


Use the Context Help window with charts and graphs

LV Basics I 19

Clusters
Data structure that groups data together Data may be of different types Analogous to record in Pascal or struct in C Elements must be either all controls or all indicators Thought of as wires bundled into a cable

LV Basics I 20

Cluster Controls and Indicators


1. Select a Cluster shell from the Array & Cluster subpalette 2. Place objects inside the shell

LV Basics I 21

Cluster Constants
Select a Cluster Constant shell from the Cluster subpalette Right-click on existing cluster and select Create>>Constant

LV Basics I 22

Cluster Order
Elements have a logical order (start with 0) To change order, right-click on border and select Reorder Controls in Cluster...

LV Basics I 23

Use Clusters to Pass Data to SubVIs


Use clusters to pass several values to one terminal Overcomes 28terminal limit Simplifies wiring

LV Basics I 24

Cluster Functions
In the Cluster subpalette of the Functions palette Can also be accessed by right-clicking on the cluster terminal

(Terminal labels reflect data type) Bundle

Bundle By Name
LV Basics I 25

Cluster Functions

Unbundle

Unbundle By Name

Unbundled cluster in the diagram


LV Basics I 26

Polymorphism with Clusters


Polymorphic functions work with clusters Arithmetic functions can perform computations on clusters of numeric data

LV Basics I 28

Exercise 5-5 on Page 5-40


Students build Cluster Exercise.vi
Approximate time to complete: 30 min.

LV Basics I 29

Exercise 5-6 on Page 6-43 (Optional)


Students build Cluster Scaling.vi
Approximate time to complete: 10 min.

LV Basics I 30

Summary
An array is a collection of elements of same data type numeric, Boolean, string, etc. Creating array or cluster controls/indicators is a two-step process 1. Get array or cluster shell 2. Place desired control/indicator inside the shell Loops can accumulate arrays at boundaries auto-indexing Array functions are in Array subpalette of Functions palette LabVIEW arithmetic functions are polymorphic input different data types Plot data on graphs Many features to manipulate plotted graph Multiple plots can be plotted on one graph Clusters group data that can be different types The Bundle and Unbundle functions are used to create and disassemble clusters
LV Basics I 31

Additional Exercises on page 5-46


5-7 Students build Reverse Random Array.vi
Time to complete: 20 min.

5-8 Students build Find Mid Value.vi


Time to complete: 20 min.

5-9 Students build Extract 2D Array.vi


Time to complete: 20 min.

5-10 (Challenge) Students build Die Roller.vi


Time to complete: 35-40 min.

5-11 (Challenge) Students build Array Pair Multiplier.vi


Time to complete: 20 min.
LV Basics I 32

Lesson 6 Case and Sequence Structures

You Will Learn: A. About Case structures

B. About Sequence structures


C. About Formula Nodes D. How to replace Sequence structures

LV Basics I 33

Case Structures
In the Structures subpalette of Functions palette Enclose nodes or drag them inside the structure Stacked like a deck of cards, only one case visible

LV Basics I 34

Boolean and Numeric Cases

Note: all possible outputs of the Case structure must be wired


LV Basics I 35

Exercise 6-1 on page 6-6


Students build Square Root.vi
Time to complete: 20 min.

LV Basics I 36

Exercise 6-2 on page 6-9


Students modify Temperature Running Average (Ex. 4-5) and save as Temperature Control.vi *This VI will be used in a later exercise.
Time to complete: 25 min.

LV Basics I 37

Sequence Structures
In the Structures subpalette of Functions palette Executes diagrams sequentially, Frame 0 (0..x), where x is the total number of frames Stacked like a deck of cards, only one frame visible

LV Basics I 38

Sequence Locals
Pass data from one frame to future frames Created at the border of the Sequence structure

Data not available

Sequence local created in Frame 1

Data available

LV Basics I 39

Exercise 6-3 on page 6-13


Students build Time to Match.vi by modifying Auto Match.vi (Ex. 4-3)
Time to complete: 30 min.

LV Basics I 40

Formula Node
In the Structures subpalette Implement complicated equations Variables created at border Variable names are case sensitive Each statement must terminate with a semicolon (;) Context Help Window shows available functions

Note semicolon

LV Basics I 41

Conditional Branching in Formula Nodes


if (x >= 0) then y = sqrt(x) else y = -99999.0 end if
Conditional Operator False Condition

Condition
LV Basics I 42

True Condition

Exercise 6-4 on page 6-18


Students build Formula Node Exercise.vi
Time to complete: 20 min.

LV Basics I 43

Replacing Sequence Structures with Case Structures

LV Basics I 44

Summary
Two structures to control flow of data Case structure Sequence structure Case structure Boolean or numeric cases selector determines type Subdiagrams placed inside case structure Output from a Case structure must be defined for all cases Sequence structure executes subdiagrams sequentially Sequence locals pass data between frames Created at the border of Sequence structure Data available in subsequent frames Formula Nodes allow direct entry of equations in the block diagram Sequence structures can be replaced with a Case inside of a Loop
LV Basics I 45

Additional Exercises on page 6-23


6-5 Students build Equations.vi
Time to complete: 20 min.

6-6 Students build Calculator.vi


Time to complete: 30 min.

6-7 Students build Square Root 2.vi


Time to complete: 20 min.

6-8 (Challenge) Students build Using Array

Over Threshold.vi
Time to complete: 30 min.
LV Basics I 46

Lesson 7 Strings and File I/O

You Will Learn:


A. How to create string controls and indicators B. Some string functions C. How to perform file input and output operations D. How to format text files for spreadsheets E. How to use the high-level File VIs

LV Basics I 47

Strings
A string is a sequence of displayable or nondisplayable characters (ASCII) Many uses displaying messages, instrument control, file I/O String control/indicator is in the Controls >> String subpalette

LV Basics I 48

String Display Modes


Normal display \ code display

Password display

Hex display

LV Basics I 49

String Functions

String Length
String

Length = 20

Concatenate Strings spaces here Strings


The quick brown fox jumped over the lazy dog.

Concatenated String

LV Basics I 50

String Functions

LV Basics I 51

String Functions
Converting numbers to strings

Converting strings to numbers

LV Basics I 52

Edit Format String


Format Into String & Scan From String Functions

LV Basics I 53

Exercise 7-1 on page 7-8


Students build Build String.vi *This VI will be used in a later exercise.
Time to complete: 25 min.

LV Basics I 54

File Input and Output


Three levels of hierarchy High-level utility file VIs Intermediate file I/O VIs Advanced file I/O functions

High Level File VIs Intermediate File VIs and Functions Advanced File Functions

LV Basics I 55

Intermediate File I/O VIs


Open/Create/Replace file opens, creates, or replaces a file Read File reads count bytes from the file Write File writes data to the file Close File closes the file Error handling in file I/O Time & Dialog subpalette Displays a dialog box if an error occurs

LV Basics I 56

Saving Data to a File

Open/Create/Replace opens the existing file TEST1.DAT and generates refnum and error cluster Write File writes the data Close File closes the file Simple Error Handler checks for errors
LV Basics I 57

Reading Data from a File

Open/Create/Replace opens the file Read File reads the specified number of bytes from the file

Close File closes the file


Simple Error Handler checks for errors
LV Basics I 58

Exercise 7-2 on page 7-17


Students build File Writer.vi
Time to complete: 20 min.

LV Basics I 59

Exercise 7-3 on page 7-19


Students build File Reader.vi
Time to complete: 20 min.

LV Basics I 60

Formatting a Spreadsheet String


Spreadsheets are popular tools for data handling and analysis There are many formats for spreadsheet data. One of the most popular is tab-delimited: Columns are separated by a tab character Rows are separated by an end-of-line character Easily created using LabVIEW file I/O VIs

LV Basics I 61

Creating a Spreadsheet File

A spreadsheet yields:

Tab
LV Basics I 62

End of Line

Exercise 7-4 on page 7-23


Students build Temperature Logger.vi by modifying Temperature Control.vi *This VI will be used in a later exercise.
Time to complete: 25 min.

LV Basics I 63

High-level File I/O VIs


Write to Spreadsheet File Read from Spreadsheet File Write Characters to File Read Characters from File Read Lines from File

LV Basics I 64

Exercise 7-5 on page 7-30


Students run Spreadsheet Example.vi
Time to complete: 25 min.

LV Basics I 65

Exercise 7-6 on page 7-34


Students build Temperature Application.vi
Time to complete: 30-40 min.

LV Basics I 66

Summary
String is a collection of ASCII characters many uses Display messages Instrument control File I/O Many functions to manipulate strings Strings palette of Functions menu Three levels of File I/O hierarchy High-level VIs Intermediate VIs and functions Advanced functions Writing data in spreadsheet format Tab character separates columns End-of-line character separates rows Write/Read from spreadsheet high-level File I/O VIs
LV Basics I 67

Additional Exercises on page 7-37


7-7 (Challenge) Students build

More

Spreadsheets.vi
Time to complete: 25 min.

7-8 (Challenge) Students build Spreadsheet

Converter.vi
Time to complete: 30 min.

7-9 Students modify Temperature Logger.vi (Ex. 7-4) and save the new VI as

Temperature Logger 2.vi


Time to complete: 25 min.
LV Basics I 68

You might also like