Anda di halaman 1dari 41

Lesson 5

Relating Data

TOPICS
A. Arrays
B. Clusters
C. Type Definitions

ni.com/training
A. Arrays

• An array consists of elements and dimensions


− Elements: data that make up the array
− Dimension: the length, height, or depth of an array
− An array can have one or more dimensions
and as many as (231)–1 elements per
dimension, memory permitting
• Consider using arrays when you work with
a collection of similar data and when you
perform repetitive computations

ni.com/training
2
A. Arrays

• The first element shown in the array (3.00) is at index 1 and


the second element (1.00) is at index 2
• The element at index 0 is not shown in this image, because
element 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

ni.com/training
3
A. Arrays – Creating

1. Place an array shell on the front panel


2. Drag a data object or element into the array shell

ni.com/training
4
A. Arrays – 2D Array

• Stores elements in a grid


• Requires a column index and a row index to locate an
element, both of which are zero-based
• To create a multidimensional array on the front panel, right-
click the index display and select Add Dimension from the
shortcut menu
• You also can resize the index display until you have as
many dimensions as you want

ni.com/training
5
A. Arrays – Initializing

• You can initialize an array, or leave it uninitialized


• For initialized arrays, you define the number of elements in
each dimension, and the contents of each element
• Uninitialized arrays have dimension but no elements

ni.com/training
6
A. Arrays – Creating Constants

• To create an array constant:


− Select an array constant on the
Functions palette
− Place the array shell on the block diagram
− Place a constant in the array shell
• You can use an array constant to store
constant data or as a basis for comparison
with another array
• Array constants also are useful for passing data into a subVI

ni.com/training
7
A. Arrays – Auto-indexing

• If you wire an array to or from a For Loop or While Loop, you


can link each iteration of the loop to an element in that array
by enabling auto-indexing on tunnel
• The tunnel changes from a solid square to the image shown
above to indicate auto-indexing

ni.com/training
8
A. Arrays – Auto-indexing Input

For Loop executes


a number of times
equal to the number
of elements in the
array

ni.com/training
9
A. Arrays – Auto-indexing Input

If the iteration count terminal is wired and arrays of different


sizes are wired to auto-indexed tunnels, the actual number of
iterations becomes the smallest of the choices.

ni.com/training
10
A. Arrays – Auto-indexing Output

• When you auto-index an array output tunnel, the output


array receives a new element from every iteration of the
loop
• Auto-indexed output arrays are always equal in size to the
number of iterations

ni.com/training
11
A. Arrays – Creating 2D Arrays

You can use two For Loops, one inside the other, to create a
2D array

ni.com/training
12
Exercise 5-1
Concept: Manipulating Arrays

Manipulate arrays using various LabVIEW functions.


GOAL
13
Exercise 5-1
Concept: Manipulating Arrays

• How would you programmatically create a 3-dimensional


array?

DISCUSSION
B. Clusters

• Clusters group data elements of mixed types


• Similar to a record or a struct in text-based programming
languages

ni.com/training
15
B. Clusters – Array vs Cluster

• Clusters differ from arrays in that they are a fixed size


• Clusters can contain mixed data types; arrays contain only
one data type
• Like an array, a cluster is either a control or an indicator and
cannot contain a mixture of controls and indicators

ni.com/training
16
B. Clusters – Creating

To create a cluster control or indicator on the front panel:


• Place a cluster shell on the front panel
• Drag a data object or element,
which can be a numeric, Boolean,
string, path, refnum, array, or
cluster control or indicator into
the cluster shell

ni.com/training
17
B. Clusters – Constants

• To create a cluster constant:


1. Select a cluster constant on the Functions palette
2. Place the cluster shell on the block diagram
3. Place a constant in the cluster shell

• If you have a cluster control or indicator, right-click the


cluster on the block diagram and select Create»Constant
from the shortcut menu

ni.com/training
18
B. Clusters – Order

• Cluster elements have a logical order unrelated to their


position in the shell
• You can view and modify the
cluster order by right-clicking
the cluster border and
selecting Reorder Controls
In Cluster from
the shortcut menu

ni.com/training
19
B. Clusters – Assembling a Cluster

Use the Bundle function to assemble a new cluster

ni.com/training
20
B. Clusters – Modifying a Cluster

Use the Bundle By Name or the Bundle function to modify an


existing cluster

ni.com/training
21
B. Clusters – Disassembling a Cluster

Use the Unbundle By Name or Unbundle function to use


individual items in a cluster

ni.com/training
22
B. Clusters – Error Clusters

• LabVIEW uses error clusters to pass error information


• An error cluster contains the following elements:
− status: Boolean value that reports True if an error occurs
− code: 32-bit signed integer that identifies the error
− source: String that identifies where the error occurred

ni.com/training
23
Exercise 5-2
Concept: Clusters

Create clusters on the front panel, reorder clusters, and use


the cluster functions to assemble and disassemble clusters.
GOAL
24
Exercise 5-2
Concept: Clusters

• What would happen if Small Cluster was reordered where


the numeric was first and the Boolean was second?

DISCUSSION
C. Type Definitions – Custom Controls

Use custom controls and indicators to extend the available set


of front panel objects
1. Create and save a
custom control
or indicator
2. Use the custom control
or indicator on other
front panels

ni.com/training
26
C. Type Definitions – Control Editor

• Use the Control Editor window to customize controls and


indicators
• To display the Control Editor
window, right-click a control
or indicator and select
Advanced»Customize

ni.com/training
27
C. Type Definitions

You can save a custom control as:


• Control
• Type Definition
• Strict Type Definition

ni.com/training
28
C. Type Definitions – Control Type

Control
• No connection between the custom control or indicator you
saved and the instance of the custom control or indicator in
the VI
• Update the file, but the
instances are not updated

ni.com/training
29
C. Type Definitions – Type Definition
Type Definition (type def):
• Edit the saved file, and have all instances update with the
changes made
• Forces the data type of each instance to be identical
• Example:
− Add an item to the type definition of an
enum, the instances of the enum
update with the new item
− If you resize the enum front panel
control type definition, the instances
of the enum do not resize

ni.com/training
30
C. Type Definitions – Strict Type Definition

Strict Type Definition:


• Similar to a type definition, except a strict type definition
forces everything about an instance to be identical to the
strict type definition, except:
− label
− description
− default value

ni.com/training
31
Exercise 5-3
Concept: Type Definition

Create a type defined enumerated control and explore the


differences between a type definition and a strict type
definition.
GOAL
32
Exercise 5-3
Concept: Type Definition

• If you only edit the color of Strict Type Def Numeric.ctl in the
Control Editor, would all the instances of the control change
color?
• If you only edit the color of Type Def Numeric.ctl in the Control
Editor, would all the instances of the control change color?

DISCUSSION
Summary—Quiz

1. You can create an array of arrays.


a) True
b) False

ni.com/training
34
Summary—Quiz Answer

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.

ni.com/training
35
Summary—Quiz

2. What is the value of the Iterations indicator


after running this VI?

ni.com/training
36
Summary—Quiz Answer

2. What is the value of the Iterations indicator


after running this VI? Value of Iterations = 4

ni.com/training
37
Summary—Quiz

3. You customize a control, select Control from the Type


Def. Status pull-down menu, and save the control as a
.ctl file. You then use an instance of the custom control
on your front panel. If you open the .ctl file and modify
the control, does the control on the front panel change?
a) Yes
b) No

ni.com/training
38
Summary—Quiz Answer

3. You customize a control, select Control from the Type


Def. Status pull-down menu, and save the control as a
.ctl file. You then use an instance of the custom control
on your front panel. If you open the .ctl file and modify
the control, does the control on the front panel change?
a) Yes
b) No

ni.com/training
39
Summary—Quiz

4. You are inputting data that represents a circle. The circle data
includes three double precision numerics: x position, y position and
radius. In the future, you might need to expand all instances of the
circle data to include the color of the circle, represented as an
integer. How should you represent the circle on your front panel?
a) Three separate controls for the two positions and the radius
b) A cluster containing all of the data
c) A custom control containing a cluster
d) A type definition containing a cluster
e) An array with three elements

ni.com/training
40
Summary—Quiz Answer

4. You are inputting data that represents a circle. The circle data
includes three double precision numerics: x position, y position and
radius. In the future, you might need to expand all instances of the
circle data to include the color of the circle, represented as an
integer. How should you represent the circle on your front panel?
a) Three separate controls for the two positions and the radius
b) A cluster containing all of the data
c) A custom control containing a cluster
d) A type definition containing a cluster
e) An array with three elements

ni.com/training
41

Anda mungkin juga menyukai