5. Data Types and StructuresLike other programming languages, NI LabVIEW software has many different data types, and ways to organize those data types. They can be distinguished by color and decoration, and they are each used for particular reasons. The following module illustrates the capabilities of various data types and structures, and when to use one over another. Four videos included in this module: Detailed ExplanationDownload the Detailed Explanation PDF String Data Type
A string is a sequence of displayable or nondisplayable ASCII characters. Strings provide a platform-independent format for information and data. Some of the more common applications of strings include the following:
-Creating simple text messages.
On the front panel, strings appear as tables, text entry boxes, and labels. LabVIEW includes built-in VIs and functions you can use to manipulate strings, including formatting strings, parsing strings, and other editing. LabVIEW represents string data with the color pink. Click here for more information on string dataNumeric Data Type
LabVIEW represents numeric data as floating-point numbers, fixed-point numbers, integers, unsigned integers, and
complex numbers. Double and Single precision as well as Complex numeric data is represented with the color orange in
LabVIEW. All Integer numeric data is represented with the color blue.
Note: The difference among the numeric data types is the number of bits they use to store data
and the data values they represent.
Certain data types also provide extended configuration options. For example, you can associate physical units of
measure with floating-point data, including complex numbers, and you can configure the encoding and range for
fixed-point data.
Click here for more information on numeric data Click here for a table of numeric data types Boolean Data Type
LabVIEW stores Boolean data as 8-bit values. A Boolean can be used in LabVIEW to represent a 0 or 1, or a TRUE or FALSE. If the 8-bit value is zero, the Boolean value is FALSE. Any nonzero value represents TRUE. Common applications for Boolean data include representing digital data and serving as a front panel control that acts as a switch that has a mechanical action often used to control an execution structure such as a Case structure. A Boolean control is typically used as the conditional statement to exit a While Loop. In LabVIEW, the color green represents Boolean data. Click here for more information about Boolean control of mechanical actions Dynamic Data Type
Most Express VIs accept and/or return the dynamic data type, which appears as a dark blue terminal. Using the Convert to Dynamic Data and Convert from Dynamic Data VIs, you can convert floating-point numeric or Boolean data of the following data types :
-1D array of waveforms
Wire the dynamic data type to an indicator that can best present the data. Indicators include a graph, chart, or numeric, or Boolean indicator. However, because dynamic data undergoes an automatic conversion to match the indicator to which it is wired, Express VIs can slow down the block diagram execution speed.
The dynamic data type is for use with Express VIs. Most other VIs and functions that are shipped with LabVIEW do not
accept this data type. To use a built-in VI or function to analyze or process the data the dynamic data type
includes, you must convert the dynamic data type.
Click here for more information on dynamic data Data Structures in LabVIEWSometimes it is beneficial to group data related to one another. Use arrays and clusters to group related data in LabVIEW. Arrays combine data of the same data type into one data structure, and clusters combine data of multiple data types into one data structure. Arrays
An array consists of elements and dimensions. Elements are the data that make up the array. A dimension is 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.
Figure 1. Array Index Display, Numeric Control Array Creating Array Controls and IndicatorsCreate an array control or indicator on the front panel by adding an array shell to the front panel, as shown in Figure 2, and dragging a data object or element, such as a numeric or string control, into the array shell.
Figure 2. Placing a Numeric Control in an Array Shell
If you attempt to drag an invalid control or indicator into the array shell, you are unable to place the control or
indicator in the array shell.
2D ArraysThe previous examples use 1D arrays. A 2D array stores elements in a grid. It requires a column index and a row index to locate an element, both of which are zero-based. Figure 3 shows an eight-column by eight-row 2D array, which contains 8 × 8 = 64 elements.
Figure 3. 2D Array
Initializing ArraysYou can initialize an array or leave it uninitialized. When an array is initialized, you defined the number of elements in each dimension and the contents of each element. An uninitialized array contains a fixed number of dimensions but no elements. Figure 4 shows an uninitialized 2D array control. Notice that the elements are all dimmed. This indicates that the array is uninitialized.
Figure 4. 2D Uninitialized Array In Figure 5, six elements are initialized. In a 2D array, after you initialize an element in a row, the remaining elements in that row are initialized and populated with the default value for the data type. For example, in Figure 5, if you enter 4 into the element in the first column, third row, the elements in the second and third column in the third row are automatically populated with a 0.
Figure 5. An Initialized 2D Array With Six Elements Creating Array ConstantsTo create an array constant on the block diagram, select an Array Constant on the Functions palette, place the array shell on the block diagram, and place a string constant, numeric constant, Boolean constant, or cluster constant in the array shell. You can use an array constant to store constant data or as a basis for comparison with another array. Using Arrays With LoopsAuto-Indexing Array InputsIf 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. The tunnel image changes from a solid square to the image to indicate auto-indexing. Right-click the tunnel and select Enable Indexing or Disable Indexing from the shortcut menu to toggle the state of the tunnel. Array Inputs
If you enable auto-indexing on an array wired to a For Loop input terminal, LabVIEW sets the count terminal to the
array size so you do not need to wire the count terminal. Because you can use For Loops to process arrays one
element at a time, LabVIEW enables auto-indexing by default for every array you wire to a For Loop. You can disable
auto-indexing if you do not need to process arrays one element at a time.
Figure 6. Array Used to Set For Loop Count
If you enable auto-indexing for more than one tunnel or if you wire the count terminal, the actual number of
iterations becomes the smaller of the choices. For example, if two auto-indexed arrays enter the loop, with 10 and
20 elements respectively, and you wire a value of 15 to the count terminal, the loop still executes only 10 times,
indexing all elements of the first array but only the first 10 elements of the second array.
Array Outputs
When you auto-index an array output tunnel, the output array receives a new element from every iteration of the
loop. Therefore, auto-indexed output arrays are always equal in size to the number of iterations. The wire from
the output tunnel to the array indicator becomes thicker as it changes to an array at the loop border, and the
output tunnel contains square brackets representing an array.
Figure 7. Auto-Indexed Output
Right-click the tunnel at the loop border and select Enable Indexing or Disable
Indexing from the shortcut menu to enable or disable auto-indexing. Auto-indexing for While Loops is
disabled by default. For example, disable auto-indexing if you need only the last value passed out of the
tunnel.
Creating 2D Arrays
You can use two For Loops, nested one inside the other, to create a 2D array. The outer For Loop creates the row
elements, and the inner For Loop creates the column elements.
Figure 8. Creating a 2D Array Clusters
Clusters group data elements of mixed types. An example of a cluster is the LabVIEW error cluster, which combines a
Boolean value, a numeric value, and a string. A cluster is similar to a record or a struct in text-based programming
languages.
Order of Cluster Elements
Although cluster and array elements are both ordered, you must unbundle all cluster elements at once using the
Unbundle function. You can use the Unbundle by Name function to unbundle cluster elements by name. If you use the
Unbundle by Name function, each cluster element must have a label. Clusters also differ from arrays in that they are
a fixed size. Like an array, a cluster is either a control or an indicator. A cluster cannot contain a mixture of
controls and indicators.
Creating ClustersControls and IndicatorsCreate a cluster control or indicator on the front panel by adding a cluster shell to the front panel, as shown in Figure 9, and dragging a data object or element, which can be a numeric, Boolean, string, path, refnum, array, or cluster control or indicator, into the cluster shell. Resize the cluster shell by dragging the cursor while you place the cluster shell.
Figure 9. Creation of a Cluster Control Figure 10 is an example of a cluster containing three controls: a string, a Boolean switch, and a numeric. A cluster is either a control or an indicator; it cannot contain a mixture of controls and indicators.
Figure 10. Cluster Control Example
Creating Cluster Constants
To create a cluster constant on the block diagram, select a Cluster Constant on the Functions
palette, place the cluster shell on the block diagram, and place a string constant, numeric constant, Boolean
constant, or cluster constant in the cluster shell. You can use a cluster constant to store constant data or as a
basis for comparison with another cluster.
Using Cluster Functions
Use the Cluster functions to create and manipulate clusters. For example, you can perform tasks similar to the
following:
Assembling Clusters
Use the Bundle function to assemble a cluster from individual elements or to change the values of individual
elements in an existing cluster without having to specify new values for all elements. Use the Positioning tool to
resize the function or right-click an element input and select Add Input from the shortcut menu.
Figure 11. Assembling a Cluster on the Block Diagram
Modifying a Cluster
|
