Lesson 5
Creating and Leveraging Data Structures
Create, manipulate and use arrays, clusters, and type definition
controls for data access and analysis.
A. Arrays D. Auto-Indexing
B. Common Array Functions E. Clusters
C. Polymorphism F. Type Definitions
1
LESSON 5 Creating and Leveraging Data Structures
A. Arrays
Identify when to use arrays and learn how to create and initialize
arrays.
• 1D and 2D Arrays
• Creating an Array Control and Constant
• Initializing Arrays
2
A. Arrays
Arrays
A collection of data elements of
the same type.
3
A. Arrays
1D and 2D Examples
1D array - One row of 10 elements
0 3.2
1.2 1 2 38.04 5 5.1
6 76.0 81.0 9 2.5 1.7
2D array - Five-row by seven-column table Index
of 35 elements numbers
0 1 2 3 4 5 6
0
1
2
3
4
4
A. Arrays
2D Arrays
2D arrays:
– Store elements in a grid.
– Require a column index and a row index to locate an element, both
of which are zero-based.
5
A. Arrays
Initializing Arrays
Uninitialized Array Initialized Array
6
LESSON 5 Creating and Leveraging Data Structures
Demonstration
Viewing Arrays
First element at
index 1 Second
The elements at index 0 are element at
not shown because element index 2
1 is selected in the index
display.
The element selected in the index display always refers to the element
shown in the upper-left corner of the element display.
7
Lesson 5 Creating and Leveraging Data Structures
B. Common Array Functions
Create and manipulate arrays using built-in array functions.
8
LESSON 5 Creating and Leveraging Data Structures
Multimedia Module
Common Array Functions
Use LabVIEW’s built-in array functions to create and manipulate arrays.
9
B. Common Array Functions
Initialize Array
10
B. Common Array Functions
Insert Into Array
11
B. Common Array Functions
Delete From Array
12
B. Common Array Functions
Array Max & Min
13
B. Common Array Functions
Search 1D Array
14
LESSON 5 Creating and Leveraging Data Structures
Activity 5-1
Using Array Functions
Complete the highlighted portion in each VI.
15
B. Common Array Functions
Activity 5-1 Answers
16
B. Common Array Functions
Activity 5-1 Answers
17
Lesson 5 Creating and Leveraging Data Structures
C. Polymorphism
Understand the ability of various VIs to accept input data of
different data types.
18
C. Polymorphism
Polymorphism
Polymorphism—The ability of VIs and functions to automatically
adapt to accept input data of different data types.
Functions are polymorphic to varying degrees:
• None, some, or all of their inputs can be polymorphic.
• Some accept numeric or Boolean values.
• Some accept numeric or strings.
• Some accept scalars, numeric arrays, or clusters of numerics.
19
C. Polymorphism
Arithmetic Functions Are Polymorphic
Combination Result
20
Lesson 5 Creating and Leveraging Data Structures
D. Auto-Indexing
Use auto-indexed inputs and outputs to create graphs and arrays.
• Use in For Loops and While Loops • Creating 2D Arrays
• Waveform Graphs • Auto-Indexing Input to a Loop
• Auto-Indexing with a Conditional
Terminal
21
D. Auto-Indexing
Auto-Indexing
• Auto-Indexing Enabled • Auto-Indexing Disabled
Wire becomes thicker Wire remains the same size
1D Array Only one value
(last iteration) is passed
0 1 2 3 4 5 out of the loop.
22
D. Auto-Indexing
Waveform Graphs
23
D. Auto-Indexing
Charts vs. Graphs—Single-Plot
24
D. Auto-Indexing
Auto-Indexing with a Conditional Tunnel
Right-click on a tunnel and select
Tunnel Mode»Conditional.
25
D. Auto-Indexing
Creating Two-Dimensional Arrays
• Inner loop creates column elements.
• Outer loop stacks column elements into rows.
2D Array
1D Array
0 1 2 3 4 5 26
D. Auto-Indexing
Auto-Indexing Input
• Use an auto-indexing input
array to perform calculations
on each element in an array.
• Wire an array to an auto-
indexing tunnel on a For Loop.
• You do not need to wire the
count (N) terminal.
27
D. Auto-Indexing
Auto-Indexing Input─Different Array Sizes
28
LESSON 5 Creating and Leveraging Data Structures
Exercise 5-1
Manipulating Arrays
Manipulate arrays using various LabVIEW functions.
29
LESSON 5 Creating and Leveraging Data Structures
Exercise 5-1
Manipulating Arrays
In the All Data Channel case, how can you verify that the two approaches yield the same
results?
30
Lesson 5 Creating and Leveraging Data Structures
E. Clusters
Identify when to use clusters and be able to create them.
• Reasons To Use Clusters • Ordering Items and Resizing Clusters
• Cluster vs. Arrays • Disassembling and Modifying
• Creating a Cluster Control and Clusters
Constant • Plotting Data
31
E. Clusters
Clusters
• Clusters group data elements of mixed types.
• Clusters are similar to a record or a struct in text-based
programming languages.
32
E. Clusters
Clusters vs. Arrays
Clusters
• Mixed data types
• Fixed size
Arrays
• One data type
• Vary in size
33
LESSON 5 Creating and Leveraging Data Structures
Demonstration
Create a Cluster Control
34
E. Clusters
Cluster Order
Cluster elements have a
logical order unrelated to
their position in the shell.
35
E. Clusters
Autosizing Clusters
• Arrange elements in clusters
• NI recommends:
– Arrange vertically
– Arrange elements compactly
– Arrange elements in
preferred order
36
E. Clusters
Disassembling Clusters
• Unbundle By Name • Unbundle
– Use if all elements have – Use if some or all cluster
names. elements are unnamed.
37
E. Clusters
Modifying a Cluster
• Bundle By Name
– access elements in a cluster.
• Bundle
– some or all cluster elements are unnamed.
You must
wire the
input
cluster
terminal.
38
LESSON 5 Creating and Leveraging Data Structures
Demonstration
Creating a Cluster on the Block Diagram
39
E. Clusters
Multi-plot Graphs/Charts and XY Graph
40
E. Clusters
Plotting Data
Use the Context Help window
to determine how to wire
multi-plot data to Waveform
Graphs, Charts, and XY
Graphs.
41
E. Clusters
Error Clusters
An error cluster contains the following elements:
– status
– code
– source
42
LESSON 5 Creating and Leveraging Data Structures
Exercise 5-2
Temperature Warnings VI─
Clusters
Create clusters, reorder clusters, and use the cluster functions to assemble and
disassemble elements.
43
LESSON 5 Creating and Leveraging Data Structures
Exercise 5-2
Temperature Warnings VI─
Clusters
What would happen if you reordered cluster elements?
What would happen if you added a cluster element to Weather Data Out?
44
Lesson 5 Creating and Leveraging Data Structures
F. Type Definitions
Identify and determine when to use a type definition, strict type
definition, or control.
45
F. Type Definitions
Control Options
You can save a custom control
as:
• Control
• Type Definition
• Strict Type Definition
46
LESSON 5 Creating and Leveraging Data Structures
Demonstration
Difference between Control, Type Def, and Strict Type Def
47
LESSON 5 Creating and Leveraging Data Structures
Demonstration
Creating and Identifying Type Definitions (Type Def)
48
LESSON 5 Creating and Leveraging Data Structures
Exercise 5-3
Temperature Warnings VI─
Type Definition
Create and modify a type-defined cluster control and use the type definition in a
calling VI and a subVI.
49
LESSON 5 Creating and Leveraging Data Structures
Exercise 5-3
Temperature Warnings VI─
Type Definition
Now that the Weather Data control is saved as a type definition, how many instances of
the cluster would need to be updated to add wind speed data?
50
LESSON 5 Creating and Leveraging Data Structures
Activity 5-2
Lesson Review
Refer to the participant guide to answer questions about what you have learned in this
lesson and then discuss the answers as a group.
51
Lesson Review
1. You can create an array of arrays.
a. True
b. False
52
Lesson Review
1. You can create an array of arrays.
a. True
b. False
You cannot drag an array data type into an array shell. However, you
can create two-dimensional arrays.
53
Lesson Review
2. What is the value of the Iterations
indicator after running this VI?
54
Lesson Review
2. What is the value of the Iterations
indicator after running this VI?
4
55
Lesson Review
3. Which of the following custom control settings
defines the data type of all instances of a control
but allows for different colors and font styles?
a. Control
b. Type definition
c. Strict type definition
d. Cluster control
56
Lesson Review
3. Which of the following custom control settings
defines the data type of all instances of a control
but allows for different colors and font styles?
a. Control
b. Type definition
c. Strict type definition
d. Cluster control
57
Lesson Review
4. You have input data representing a circle: X Position (I16), Y Position (I16),
and Radius (I16). In the future, you might need to modify your data to
include the color of the circle (U32).
What data structure should you use to represent the circle in your
application?
a. Three separate controls for the two positions and the radius
b. A cluster containing all of the data (X,Y)
c. A custom control containing a cluster
d. A type definition containing a cluster R
e. An array with three elements
58
Lesson Review
4. You have input data representing a circle: X Position (I16), Y Position (I16),
and Radius (I16). In the future, you might need to modify your data to
include the color of the circle (U32).
What data structure should you use to represent the circle in your
application?
a. Three separate controls for the two positions and the radius
b. A cluster containing all of the data (X,Y)
c. A custom control containing a cluster
d. A type definition containing a cluster R
e. An array with three elements
59