Download presentation
Presentation is loading. Please wait.
Published byJob Rogers Modified over 6 years ago
2
Data Structures Arrays Common Array Functions Polymorphism
Auto-Indexing Clusters Type Definitions Plotting Data – Charts and Graphs
3
A. Arrays 1D and 2D Arrays Creating an Array Control and Constant
Initializing Arrays
4
Arrays An array: Is a collection of data elements that are of same type. Has one or more dimensions. Contains up to (231)–1 elements per dimension, memory permitting. Accesses elements by its index. Note: The first element is index 0. Arrays are lists of elements of the same data type. They are analogous to arrays in traditional languages. Arrays can have one or more dimensions. Arrays can have up to (2^31)-1 elements per dimension. Actual array sizes that students can create is limited by memory. Elements are accessed by an index. The index ranges from 0 to N-1 (where N = number of elements in the array). Arrays are zero-indexed (first element is zero) in each dimension. A 2D array is analogous to a spreadsheet or table. Example: If your data contains temperature readings and time stamps, one column is time values and the other column is readings. Be careful to specify the element you really want. Example: The first element in an array is array(0), not array(1).
5
Arrays – 1D and 2D Examples
1D array One row of 10-elements 1.2 3.2 8.2 8.0 4.8 5.1 6.0 1.0 2.5 1.7 2D array Five-row by seven-column table of 35 elements Index numbers Point to a specific element and ask students which index (which indices) this element is at. Emphasize that for 2D arrays, the row index in specified first, and then the column index follows. 1 2 3 4
6
Why Use Arrays? Use arrays when you work with a collection of similar data and when you perform repetitive computations.
7
Creating an Array Control
For a new array: Select an Array control from the Controls palette on the front panel. Place a data object, such as a numeric control, into the array shell. Add more dimensions, if necessary, by resizing the index display. From a block diagram terminal or wire: Right-click the object and select Create»Control or Create»Indicator. Creating array controls in LabVIEW: You can place any data type in an array shell except an array. You cannot have an array of arrays; use a 2D array instead. Emphasize that this is a two-step process. Students often place only empty array shells on the front panel. Remind them they must place a data type inside the array shell. Demonstrate the following on your computer: Create a numeric array. Point out index and data object components. Show how to create a 2D array. Show how to display multiple array elements. Show that index elements always reference the upper-leftmost object in the array display. Show how elements in an array are initially grayed out, indicating that a portion of the array has not been defined.
8
Creating an Array Constant
For a new array: Select Array Constant from the Functions palette on the block diagram. Place a constant, such as a numeric, into the array shell. Add more dimensions, if necessary, by resizing the index. From a block diagram terminal or wire: Right-click and select Create»Constant. Creating array constants uses a two-step process. Select Array Constant from the Functions palette. Place a constant data type inside the empty array shell. Note again that the data types are grayed-out and must be manually defined if the user wants to store values in the array constant.
9
2D Arrays 2D arrays: Store elements in a grid. Require a row index and a column index to locate an element, both of which are zero-based. Create a multidimensional array on the front panel by right-clicking the index display and selecting Add Dimension from the shortcut menu, or: Resize the index display until you have as many dimensions as you want.
10
Viewing Arrays on the Front Panel
First element at index 1 The element at index 0 is not shown because element 1 is selected in the index display. Second element at index 2 The element selected in the index display always refers to the element shown in the upper-left corner of the element display. The control or indicator representing an array on the front panel can be viewed as a „window” through which we can see a fragment of the whole array, which lies behind that „window” and is possibly of different size (bigger or smaller) than the „window”. The array can be scrolled under the „window” by changing the index number displayed in index display. 9
11
Initializing Arrays 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.
12
B. Common Array Functions
Array Size Initialize Array Array Subset Build Array Index Array
13
Common Array Functions
Array Size Initialize Array Array Subset Build Array Index Array LabVIEW has many functions to manipulate arrays. These functions are located on the Array subpalette of the Functions palette. Instructor: Choose one of the following approaches to help students understand how to use these common array functions. Walk through the following slides. Create demos on the fly. Show related shipping examples. For example, Build Array.vi has more combinations than shown in the following slides.
14
Array Size The Array Size function returns the number of elements in the array. 1D array: Output is a numeric. It indicates the number of elements in the array. Multidimensional array: Array output has elements signifying size of each dimension. For example: a 2 x 3 array, as in the slide, will output an array of two elements. The first element = 2 and second element = 3. The first number is the number of rows (2) and the second element is the number of columns (3).
15
Initialize Array The Initialize Array function creates an n-dimensional array in which every element is initialized to the value of element. Initialize array is a resizable node. Use the Positioning tool (drag the lower border of the function node) to resize the function and increase the number of dimensions (element, row, column, page, and so on) of the output array. You must have one dimension size input terminal for each dimension in the array.
16
Array Subset The Array Subset function returns a portion of an array starting at index and containing length number of elements.
17
Build Array The Build Array function concatenates elements together into one array or concatenates multiple arrays together into arrays of higher dimension. Wiring a scalar element into the Build Array function automatically configures the Build Array function to concatenate elements. When wiring in two or more arrays, right click on the output of the Build Array function to enable or disable concatenated inputs. The Build Array function is a resizable node and can be resized to add additional input terminals. (Drag the lower border of the function node to resize it.)
18
Index Array The Index Array function: Returns the element or subarray of n-dimension array at index. When you wire an array to this function, the function resizes automatically to display index inputs for each dimension in the array you wire to n-dimension array. The slide shows examples of the function accessing a single element of a one-dimensional array, a row of a two-dimensional array, and a column of a two-dimensional array. Index Array does not remove the element from the array. Remind students that arrays start their indices at zero. Index 2 in the example actually accesses the third element in the array.
19
C. Polymorphism
20
Polymorphism Functions are polymorphic to varying degrees:
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. Functions are polymorphic to varying degrees—none, some, or all of their inputs can be polymorphic. Some function inputs accept numeric values or Boolean values. Some accept numeric values or strings, etc. Some accept not only scalar numeric values but also arrays of numeric values, clusters of numeric values, arrays of clusters of numeric values, and so on. Some accept only one-dimensional arrays although the array elements can be of any type. Some functions accept all types of data, including complex numeric values.
21
Arithmetic Functions Are Polymorphic
Combination Result LabVIEW arithmetic functions are polymorphic. Inputs to arithmetic functions can be of different data types. The function automatically performs the appropriate operation on unlike data types. LabVIEW arithmetic functions greatly simplify array arithmetic. Examples of polymorphism on this slide: Scalar+Scalar: Scalar addition. Scalar+Array: The scalar is added to each element of array. Array+Array: Each element of one array is added to the corresponding element of other array. In case students ask, polymorphism does not perform matrix arithmetic when inputs are 2D arrays. (For example, multiplying two 2D array inputs with the Multiply function performs an element by element multiplication, not matrix multiplication). LabVIEW ships with other examples of arithmetic functions showing polymorphic combinations of inputs. Open Example – Polymorphism and Manipulating Arrays VI to see that the addition of a scalar to an array yields the same result as adding the scalar to each element of the array separately. Notice that the polymorphic abilities of functions allow us to achieve the desired results with very little code (the addition of an array and a scalar requires only the ‘Add’ function, there is no need to use a loop etc.).
22
D. Auto-Indexing Use in For Loops and While Loops
Auto-Indexing with a Conditional Terminal Creating 2D Arrays Auto-Indexing Input to a Loop
23
Auto-Indexing Allows loops to accumulate arrays at their boundaries.
Auto-Indexing Enabled Allows loops to accumulate arrays at their boundaries. Is the default behavior for For Loops. Is disabled by default for While Loops. Is enabled/disabled by right-clicking on a tunnel. Produces arrays that are always equal in size to the number of iterations of the loop. Wire becomes thicker 1D Array Auto-Indexing Disabled Wire remains the same size For Loops and While Loops can index and accumulate arrays at their boundaries. This is known as auto-indexing. The indexing point on the boundary is called a tunnel. The For Loop default is auto-indexing enabled. The While Loop default is auto-indexing disabled. Only one value (the last iteration) is passed out of the While Loop by default. Examples: Enable auto-indexing to collect values within the loop and build the array. All values are placed in the array upon exiting the loop. Disable auto-indexing if you are interested only in the final value. Only one value (last iteration) is passed out of the loop
24
Auto-Indexing with a Conditional Tunnel
Right-click on a tunnel and select Tunnel Mode»Conditional. You can determine what values LabVIEW writes to the loop output tunnel based on a condition you specify by right-clicking the loop output tunnel and selecting Tunnel Mode»Conditional from the shortcut menu. Because of the conditional tunnel, the Values less than 5 array contains only the elements 2, 0, 3, and 1 after this loop completes all iterations. Note: „Conditional” option for tunnels is available since LabVIEW For older LabVIEW versions, the same functionality may be achieved using a Case structure with Build Array function and a shift register to store the array.
25
Creating 2D Arrays 1D Array 2D Array You can use two nested For Loops to create a 2D array. Auto-indexing must be enabled for both loops. Explain the different line thicknesses in the wire connecting the Random Number function to the 2D Array Indicator. Demonstrate on your computer: how to enable/disable indexing how this changes wire thickness. Inner loop creates column elements (creates single rows). Outer loop stacks column elements into a 2D array.
26
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. LabVIEW does not exceed an array size. This helps to protect against programming errors. If you wire a 10-element array and a 5-element array to a For loop, the loop iterates five times. Note: Although the For loop iterates 5 times, the number of iterations is zero-based. Therefore, the final value of the Iterations indicator is 4. 25
27
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. The For Loop executes the number of times equal to the number of elements in the array. The Run button is not broken. 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. Normally, if the count terminal of the For Loop is not wired, the Run arrow is broken. In this slide, the For Loop executes a number of times equal to the number of elements in the array. Use auto-indexing on input arrays for calculations to be performed on each element of array. To pass the entire array into a loop, disable auto-indexing. 26
28
Concept: Polymorphism and Manipulating Arrays
Build a VI where an array and a scalar are added in two ways: Polymorphic Add of array + scalar Auto-indexed array elements added to scalar and verify that the results are the same. You can use the comparison and Boolean functions to compare if both resulting arrays are the same. Comparison functions are also polymorphic – they accept scalar and array inputs of various data types. Use the Equal? function followed by the And Array Elements function. Or, use only the Equal? function, right-click it and select Comparison Mode » Compare Aggregates. Note: Typically, you can’t perform a direct comparison on double-precision values. You would need to compare the values within some acceptable range. However, in this case, the polymorphic Add operation should be an exact equivalent to the Add function in an auto-indexed For loop.
29
E. Clusters Reasons To Use Clusters Clusters vs. Arrays
Creating a Cluster Control and Constant Ordering Items and Resizing Clusters Disassembling and Modifying Clusters
30
Clusters Clusters group data elements of mixed types.
Clusters are similar to a record or a struct in text-based programming languages. Additional items to mention: Bundling several data elements into clusters eliminates wire clutter on the block diagram and reduces inputs and outputs for subVIs. Most clusters on the block diagram have a pink wire and data type terminal. Clusters of numeric values, sometimes referred to as points, have a brown wire and data type terminal. You can wire brown numeric clusters to Numeric functions to perform the same operation simultaneously on all elements of the cluster. 29
31
Why Use Clusters? Keep data organized.
Logically group related data values together. Improve diagram readability by eliminating wire clutter. Reduce the number of connector pane terminals.
32
Clusters vs. Arrays A cluster has a fixed number of elements.
One cluster can contain mixed data types. A cluster can contain another cluster directly. A cluster can be a control, an indicator, or a constant. All cluster elements have to be controls, indicators, or constants. An array can vary in size. One array contains only one data type. An array cannot contain another array directly. An array can be a control, an indicator, or a constant. All array elements have to be controls, indicators, or constants. 31
33
Create a Cluster Control
For a new cluster: On the front panel, select Cluster from the Controls palette. Place a data object into the cluster shell. Place additional data objects, if necessary, into the shell. From block diagram terminal or wire: Right-click and select Create»Control or Create»Indicator. 32
34
Create a Cluster Constant
For a new cluster: On the block diagram, select Cluster Constant from the Functions palette. Place a constant into the cluster shell. Place additional data objects, if necessary, into the cluster shell. From block diagram terminal or wire: Right-click and select Create»Constant. 33
35
Autosizing Clusters Autosizing helps you arrange elements in clusters.
NI recommends the following: Arrange cluster elements vertically. Arrange elements compactly. Arranges elements in their preferred order.
36
Cluster 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. Notes: The first object you place in the cluster is element 0, the second is element 1, and so on. If you delete an element, the order adjusts automatically. The cluster order determines the order in which the elements appear as terminals on the Bundle and Unbundle functions on the block diagram. Use AutoSizing»Arrange Horizontally or AutoSizing»Arrange Vertically from the shortcut menu to arrange the elements in the cluster horizontally or vertically in order.
37
Disassembling a Cluster
Use the Unbundle By Name function whenever possible. Use Unbundle function when some or all cluster elements are unnamed. In this slide, the Polynomial Plot.vi outputs an XY Graph as a cluster of two arrays (X and Y). The Y array is labeled as P(A) so you can access it using the Unbundle By Name function. However, the X array is unlabeled so you cannot access it using the Unbundle By Name function. To access data in the X array, use the Unbundle function.
38
Modifying a Cluster Use Bundle By Name whenever possible to access elements in a cluster. Use Bundle when some or all cluster elements are unnamed. You must wire the input cluster terminal. In this slide, both examples modify the Y array, (P(A), of the Polynomial Plot.vi with a Butterworth filter. In the bottom example, you also increment the X array of the Polynomial Plot.vi output by one.
39
Creating a Cluster on the Diagram
Use the Bundle function to programmatically create a cluster on a block diagram. If the elements that are bundled have labels, you can access them using the Unbundle By Name function. Otherwise use the Unbundle function. This technique is typically used to create multi-plot charts which is discussed in more detail later.
40
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.
41
Demonstration: Clusters
Create clusters, reorder clusters, and use the cluster functions to assemble and disassemble elements.
42
Group Exercise Concept: Bundle and Unbundle Functions
What happens to VIs using your cluster if you reorder cluster elements: if you use Bundle By Name and Unbundle By Name in your VIs? if you use Bundle and Unbundle in your VIs? What would happen if you reordered cluster elements? The VIs would continue to work as expected if the only cluster functions used are Unbundle By Name and Bundle By Name. A change to element order in a cluster does not break these functions, because names of elements in the cluster remain the same. If you use Bundle and Unbundle, a change to element order in a cluster is reflected in the order of elements displayed by these functions and your code might get broken.
43
Group Exercise Concept: Editing Cluster Elements
You perform some operations on a cluster, e.g. replace one element as in the picture above. What happens if you add a cluster element to Output cluster (by editing the front panel indicator)? What would happen if you added a cluster element to Output cluster? The VI would be broken because of mismatching cluster data types – „Cluster” and „Output cluster” would not be the same data types anymore. However, if you modify both clusters in the same way, the VI is runnable again. This exercise shows why type definitions are so useful – which is covered in the next slides.
44
F. Type Definitions Type Definitions and Custom Data Types
Creating and Identifying Type Definitions Controls Strict Type Definitions
45
Type Definition (Type Def)
A type definition is a master copy of a custom data type (control, indicator, or constant). A custom data type is saved in a .ctl file. Instances of a type def are linked to the .ctl file. Instances can be controls, indicators, or constants. When the type def changes, instances automatically update. Changes include data type changes, elements added, elements removed, and adding items to an enum. You can use type definitions to define custom arrays, clusters, enums. A type definition is a master copy of a custom data type (custom control, indicator, or constant) that multiple VIs can use. When you use type definitions, you can update of all instances or copies of the custom data type in a single edit.
46
Creating a Type Definition (Type Def)
Right-click a control, indicator or constant and select Make Typedef. Right-click the object again and select Open Type Def. Edit control, if needed. Save control as a .ctl file.
47
Identifying Type Definitions (Type Def)
Look for a glyph marking the upper left corner of terminals and constants. Hover cursor over glyph to view tip strip. View Context Help while hovering cursor over terminal or constant.
48
Demonstration: Type Definition
Create and modify a type-defined cluster control. Use the type definition in a calling VI and a subVI. This exercise explores the differences between a type definition and a strict type definition.
49
Other Control Options You can save a custom control as: Control
Type Definition Strict Type Definition LabVIEW has three kinds of custom controls (.ctl files): A regular custom control Used to create controls that behave like existing controls but look different. A type def or type definition control Used for changing all instances of a linked control in a single edit. A strict type def control Used for changing instances in a single edit. Used to ensure all instances have same appearance. 48
50
Control Instances are not linked to a.ctl file.
Each instance is an independent copy of the control. Used to create controls that behave like existing controls but look different. Refer to LabVIEW Help for information on how to use the Control Editor to customize controls. Typically, if you create a custom control you want it linked to the .ctl file as a type definition.
51
Strict Type Definition
Strict type definitions are similar to a type definition in that: All instances link to .ctl file. When attributes or data types change, all instances update. Examples: Changing a knob to a dial, a round LED to a square LED, or a double to an integer. Strict type definitions enforce every aspect of a instance except label, description, and default value. Use strict type definitions to ensure all front panel instances have the same appearance. 50
52
.ctl File Options Summary
Control No connection between the one you saved and the instance in the VI Update to the file will not update any instances Type Def Connection between the saved file and all instances Forces the data type of each instance to be identical (clusters, enum) Changes made to file will populate throughout each instance Strict Type Def Connection between saved file and all instances Forces everything about an instance to be identical to the strict type definition, except: label description default value A custom control, type def or strict type def all create a *.ctl file, which stores the customizations you make to a front panel control. The thing that is special about type defs and strict type defs is that every instance of that control you use on your block diagram is linked to the *.ctl and will update if you make changes to the file. The purpose of a *.ctl file saved as a Custom Control is to prevent you from going through the potentially lengthy process of creating it again- the file provides easy access to create another. The purpose of a type def is to make sure that the data type is consistent (including the item list for an enum - which is particularly helpful when building state machines) for each instance. Strict typ defs are just that, strict. Everything must be the same (most noticeable is cosmetic changes – size of control, color, font etc). To be able to tell them apart, the label should be different (labels should always be unique and descriptive). Descriptions can be different because they might serve slightly different purposes (LED indicating different warnings in a system – you would want to be able to describe which LED represents which process more than just using the label). Last, the default value can be different. Everything else is the same. *.ctl *.ctl *.ctl
53
Group Exercise Concept: Type Definitions
If your cluster is saved as a type definition and you use this cluster in five places in your VIs, how many instances of the cluster would need to be updated to add more cluster elements? One. You add elements to the type definition. Apply and save changes. Converting from a cluster to a type definition requires a one-time update to each cluster. However, once you have your data in a type definition, adding or removing elements is a simple update to the type definition control. To avoid updating existing cluster instances to typedef clusters, just get used to creating typedefs of your custom data types at the beginning of writing your code, before you start using these data types in multiple places in your code.
54
G. Plotting Data – Charts and Graphs
Waveform Chart Waveform Graph XY Graph Multiplot Charts and Graphs
55
Waveform Chart Waveform chart is a special type of numeric indicator.
Waveform charts display single or multiple plots. Special type of numeric indicator that displays one or more plots of data, typically acquired at a constant rate.
56
Waveform Chart Properties
Extensive plot customization lets you: Show or hide legends. Change color and line styles. Change interpolation styles.
57
Waveform Chart – Visible Items
Charts also have elements that can be made visible on the front panel so the user can change chart appearance when the application is running. These are e.g.: Plot Legend Scale Legend Graph Palette. Plot legend: Use the Plot legend to customize how a plot appears in chart. Examples: line & point style, interpolation, color, fill. Scale Legend: Use the scale legend to label scales and configure scale properties on the chart. Examples: autoscale on/on, set scale format, precision, scale visibility, grid color. Graph Palette: Use the graph palette to interact with the chart as the VI runs. Examples: panning tool, zoom.
58
Chart Update Modes Right-click the chart and select Advanced»Update Mode from the shortcut menu. Strip chart is the default update mode. Scope chart and sweep chart modes display plots significantly faster than the strip chart mode. Strip chart – once the right side of the plot area is reached, the next point added causes the entire plot to shift to the left, as if it were scrolling. Scope chart – once the right side of the plot area is reached, the next point added causes the plot to disappear and the new point is added to the far left side. Oscilloscope screens update in a similar way. Sweep chart – once the right side of the plot area is reached, the next point added causes the plot to remain and the new point is added to the far left side; a red bar shows where new data is overwriting the old data. The red vertical line „sweeps” the old data out of the plot. EKG monitors update in a similar way.
59
Waveform Graph Is a graphical display of data.
Displays one or more plots of evenly sampled measurements. Is used to plot pre- generated arrays of data. Can display plots with any number of data points.
60
Charts vs. Graphs – Single Plot
Waveform Graph is used to plot pre-generated data. You generate all data samples first and then display them all at once in the graph. Thus, Waveform Graph indicators are usually updated outside the loop. Waveform Chart appends the input it receives to the history of samples collected so far. Thus, Waveform Charts are usually updated inside a loop and used to plot data samples in real time.
61
XY Graph Is a graphical display of data.
Can display plots such as circular shapes or waveforms with a varying time base. Data points may be evenly sampled or not. Is used to plot pre-generated arrays of data. Can display plots with any number of data points.
62
Charts vs. Graphs – Multi-plot and XY Graph
The Bundle function is often used to create XY graphs and multi-plot charts. The Build Array function is used to create multi-plot waveform graphs.
63
Plotting Data Use the Context Help window to determine how to wire data (including multi-plot data) to Waveform Graphs, Waveform Charts and XY Graphs. Use the Context Help and shipping examples to investigate all the possible ways to assemble data for various graphical displays. LabVIEW examples and help provide extensive information on wiring data to the various graphical displays.
64
Homework: Data Structures
Practice creating and using: arrays clusters custom controls and type definitions. The instructions are provided on the website.
65
Homework: Lotto Utilize your knowlegde about arrays, For and While loops, and Case Structures to implement a ”Pick 6” lottery. The instructions are provided on the website.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.