Lesson 5 - Data Structures

Embed Size (px)

Citation preview

  • 8/17/2019 Lesson 5 - Data Structures

    1/63

    TOPICS

    ni.com/training

    Lesson 5

    Creating and Leveraging Data Structures

    A. Arrays

    B. Common Array Functions

    C. Polymorphism

    D. Auto-Indexing

    E. Clusters

    F. Type Definitions

  • 8/17/2019 Lesson 5 - Data Structures

    2/63

    ni.com/training

    A. Arrays

    D and !D ArraysCreating an Array Control and Constant

    Initiali"ing Arrays

  • 8/17/2019 Lesson 5 - Data Structures

    3/63

    ni.com/training

    Arrays

    An array#• Is a collection of data elements

    that are of same type.

    $as one or more dimensions.• Contains up to %!&'( elements

    per dimension) memory

    permitting.

    • Accesses elements *y its index.

    +ote# The first element is index ,.

  • 8/17/2019 Lesson 5 - Data Structures

    4/63

    ni.com/training

    ID arrayOne row of 1!e"ements

    1.2 3.2 8.2 8.0 4.8 5.1 6.0 1.0 2.5 1.7

    0 1 2 3 4 5 6 7 8 9

    #D array$ive!row %y seven!co"umn ta%"e of &5e"ements

    0 1 2 3 4 5 6

    01

    2

    3

    4

    Arrays ' 1D and #D ()am*"es

    Inde)

     num%ers

  • 8/17/2019 Lesson 5 - Data Structures

    5/63

    ni.com/training

    +,y -se Arrays

    se arrays hen you or/ ith a collection of similar data and

     hen you perform repetiti0e computations.

  • 8/17/2019 Lesson 5 - Data Structures

    6/63

    ni.com/training

    iewing Arrays on t,e $ront Pane"

    The element selected in the index display alays refers to the

    element shon in the upper-left corner of the element display.

    First element

    at index 1econdelement at

    index !

    The elements at

    index , are not

    shon *ecause

    element is selectedin the index display.

  • 8/17/2019 Lesson 5 - Data Structures

    7/63

    ni.com/training

    Creating an Array Contro"

    For a ne array#. 1elect an Array control from the

    Contro"s palette on the front panel.

    !. Place a data o*2ect) such as a

    numeric control) into the arrayshell.

    &. Add a second dimension) if necessary) *y

    resi"ing the index.

    From a *loc/ diagram terminal or ire#3. 4ight-clic/ the o*2ect and select CreateContro" 

    or CreateIndicator.

  • 8/17/2019 Lesson 5 - Data Structures

    8/63

    ni.com/training

    Creating an Array Constant

    For a ne array#. 1elect Array Constant from the $unctions 

    palette on the *loc/ diagram.

    !. Place a constant) such as a numeric)

    into the array shell.&. Add a second dimension) if necessary) *y

    resi"ing the index.

    From a *loc/ diagram terminal or ire#

    3. 4ight-clic/ and select CreateConstant.

  • 8/17/2019 Lesson 5 - Data Structures

    9/63

    ni.com/training

    Initia"i2ing Arrays

    • 5ou can initiali"e an array or lea0e it uninitiali"ed.• For initiali"ed arrays) you define the num*er of elements in

    each dimension and the contents of each element.

    ninitiali"ed arrays ha0e dimension *ut no elements.

  • 8/17/2019 Lesson 5 - Data Structures

    10/63

    ni.com/training

    #D Arrays

    • !D arrays#

    6 1tore elements in a grid.

    6 4e7uire a column index and a ro index to locate an element)

    *oth of hich are "ero-*ased.• Create a multidimensional array on the front panel *y right-

    clic/ing the index display and selecting Add Dimension

    from the shortcut menu.

    • 4esi"e the index display until you ha0e as many dimensions

    as you ant.

  • 8/17/2019 Lesson 5 - Data Structures

    11/63

    ni.com/training

    3. Common Array $unctions

    Array 1i"eInitiali"e Array

    Array 1u*set

    Build ArrayIndex Array

  • 8/17/2019 Lesson 5 - Data Structures

    12/63

    ni.com/training

    Common Array $unctions

    • Array 1i"e

    • Initiali"e Array

    • Array 1u*set

    • Build Array

    • Index Array

  • 8/17/2019 Lesson 5 - Data Structures

    13/63

    ni.com/training

    Array Si2e

  • 8/17/2019 Lesson 5 - Data Structures

    14/63

    ni.com/training

    Initia"i2e Array

  • 8/17/2019 Lesson 5 - Data Structures

    15/63

    ni.com/training

    Array Su%set

  • 8/17/2019 Lesson 5 - Data Structures

    16/63

    ni.com/training

    3ui"d Array

  • 8/17/2019 Lesson 5 - Data Structures

    17/63

    ni.com/training

    Inde) Array

  • 8/17/2019 Lesson 5 - Data Structures

    18/63

    ni.com/training

    C. Po"ymor*,ism

  • 8/17/2019 Lesson 5 - Data Structures

    19/63

    ni.com/training

    Po"ymor*,ism

    Po"ymor*,ism ! T,e a%i"ity of Is and functions toautomatica""y ada*t to acce*t in*ut data ofdifferent data ty*es

    Functions are polymorphic to 0arying degrees#

    • +one) some) or all of their inputs can *e polymorphic.

    • 1ome accept numeric or Boolean 0alues.

    • 1ome accept numeric or strings.

    1ome accept scalars) numeric arrays) or clusters ofnumerics.

  • 8/17/2019 Lesson 5 - Data Structures

    20/63

    ni.com/training

    Arit,metic $unctions Are Po"ymor*,icCom%ination 4esu"t

  • 8/17/2019 Lesson 5 - Data Structures

    21/63

    ni.com/training

    D. Auto!Inde)ing

    se in For 8oops and 9hile 8oops9a0eform :raphs

    Auto-Indexing ith a Conditional Terminal

    Creating !D ArraysAuto-Indexing Input to a 8oop

  • 8/17/2019 Lesson 5 - Data Structures

    22/63

    ni.com/training

    Auto!Inde)ing+ire %ecomes t,icer

    +ire remains t,e same si2e

    Auto!Inde)ing Disa%"ed

    Auto!Inde)ing (na%"ed

    On"y one va"ue 6"ast

    iteration7 is *assed out of

    t,e "oo*

    1D Array

    1 # & 8 5

    • Allos For 8oops and 9hile8oops to accumulate arrays at

    their *oundaries.

    • Is the default *eha0ior for

    For 8oops.• Is disa*led *y default for

    9hile 8oops.

    • Is ena*led;disa*led *y right-

    clic/ing on a tunnel.• Produces arrays that are alays

    e7ual in si"e to the num*er of

    iterations of the loop.

  • 8/17/2019 Lesson 5 - Data Structures

    23/63

    ni.com/training

    +aveform 9ra*,• Is a graphical display of data.

    • Displays one or more plots of

    e0enly sampled measurements.

    • Is used to plot pre-generated

    arrays of data.

    • Can display plots ith any

    num*er of data points.

  • 8/17/2019 Lesson 5 - Data Structures

    24/63

    ni.com/training

    C,arts vs. 9ra*,s ' Sing"e!P"ot

  • 8/17/2019 Lesson 5 - Data Structures

    25/63

    ni.com/training

    Auto!Inde)ing wit, a Conditiona" Tunne"

    4ight-clic/ on a tunnel and select

    Tunne" :odeConditiona".

  • 8/17/2019 Lesson 5 - Data Structures

    26/63

    ni.com/training

    •  Inner "oo* creates co"umn e"ements.

    •  Outer "oo* stacs co"umn e"ements into rows.

    Creating #D Arrays

    1D Array

    1 # & 8 5

    #D Array

  • 8/17/2019 Lesson 5 - Data Structures

    27/63

    ni.com/training

    Auto!Inde)ing In*ut

    If the iteration count terminal is ired and arrays of differentsi"es are ired to auto-indexed tunnels) the actual num*er of

    iterations *ecomes the smallest of the choices.

  • 8/17/2019 Lesson 5 - Data Structures

    28/63

    ni.com/training

    Auto!Inde)ing In*ut

    se an auto-indexing input array to perform calculationson each element in an array.

    • 9ire an array to an auto-indexing tunnel on a For 8oop.

    5ou do not need to ire the count %+' terminal.6 The For 8oop executes

    the num*er of times

    e7ual to the

    num*er ofelements in the array.

    6 The 4un *utton is not *ro/en.

  • 8/17/2019 Lesson 5 - Data Structures

    29/63

    9OAL

    ()ercise 5!1

    Conce*t= :ani*u"ating Arrays

  • 8/17/2019 Lesson 5 - Data Structures

    30/63

    DISC-SSIO?

    ()ercise 5!1

    Conce*t= :ani*u"ating Arrays

    • In the All Data Channel case) ho can you 0erify that the

    to approaches yield the same results> – Polymorphic Add of array ? scalar

     – Auto-indexed array elements added to scalar

  • 8/17/2019 Lesson 5 - Data Structures

    31/63

    ni.com/training

    (. C"usters

    4easons To se ClustersCluster 0s. Arrays

    Creating a Cluster Control and Constant

    @rdering Items and 4esi"ing ClustersDisassem*ling and

  • 8/17/2019 Lesson 5 - Data Structures

    32/63

    ni.com/training

    C"usters

    • Clusters group data elements of mixed types.• Clusters are similar to a record or a struct in text-*ased

    programming languages.

  • 8/17/2019 Lesson 5 - Data Structures

    33/63

    ni.com/training

    +,y -se C"usters

    • eep data organi"ed.6 8ogically group related data 0alues together.

    6 Impro0e diagram reada*ility *y eliminating ire clutter.

    • 4educe the num*er of connector pane terminals.

  • 8/17/2019 Lesson 5 - Data Structures

    34/63

    ni.com/training

    C"usters vs. Arrays• Clusters are a fixed si"e.

    • Clusters can contain mixed

    data types.

    • Clusters can *e a control) an

    indicator) or a constant.6 All elements ha0e to *e controls)

    indicators) or constants.

    • Arrays 0ary in si"e.

    • Arrays contain only one data

    type.

    • Arrays can *e a control) an

    indicator) or a constant.

  • 8/17/2019 Lesson 5 - Data Structures

    35/63

    ni.com/training

    Create a C"uster Contro"

    For a ne cluster#

    . @n the front panel) select Cluster

    from the Contro"s palette.

    !. Place a data o*2ect into the

    cluster shell.

    &. Place additional data o*2ects)

    if necessary) into the shell.

    From *loc/ diagram terminal or ire#

    3. 4ight-clic/ and select CreateContro" or CreateIndicator.

  • 8/17/2019 Lesson 5 - Data Structures

    36/63

    ni.com/training

    Create a C"uster Constant

    For a ne cluster#

    . @n the *loc/ diagram) select Cluster

    Constant from the $unctions palette.

    !. Place a constant into the cluster shell.

    &. Place additional data o*2ects) ifnecessary) into the cluster shell.

    From *loc/ diagram terminal or ire#

    3. 4ight-clic/ and select CreateConstant.

  • 8/17/2019 Lesson 5 - Data Structures

    37/63

    ni.com/training

    C"uster Order

    •. Cluster elements ha0e a logicalorder unrelated to their position

    in the shell.

    •. 5ou can 0ie and modify the

    cluster order *y right-clic/ingthe cluster *order and

    selecting 4eorder Contro"s

    In C"uster.

  • 8/17/2019 Lesson 5 - Data Structures

    38/63

    ni.com/training

    Autosi2ing C"usters

    • Autosi"ing helps youarrange elements in

    clusters.

    • +I recommends the

    folloing#

    6 Arrange cluster elements

    0ertically.

    6 Arrange elements compactly.

    6 Arranges elements in their

    preferred order.

  • 8/17/2019 Lesson 5 - Data Structures

    39/63

    ni.com/training

    Disassem%"ing a C"uster

    •  se the n*undle By +ame function hene0er possi*le.•  se n*undle function hen some or all cluster elements

    are unnamed.

  • 8/17/2019 Lesson 5 - Data Structures

    40/63

    ni.com/training

    :odifying a C"uster

    • se Bundle By +ame hene0er possi*le to access

    elements in a cluster.

    • se Bundle hen some or all cluster elements are

    unnamed.

    5ou must

     ire the

    in*ut

    c"uster terminal.

  • 8/17/2019 Lesson 5 - Data Structures

    41/63

    ni.com/training

    Creating a C"uster on t,e Diagram

    •  se the Bundle function to programmatically create acluster on a *loc/ diagram.

    •  If the elements that are *undled ha0e la*els) you can

    access them using the n*undle By +ame function.

    @therise use the n*undle function.

  • 8/17/2019 Lesson 5 - Data Structures

    42/63

    ni.com/training

    C,arts vs. 9ra*,s ' :u"ti!*"ot and @ 9ra*,

  • 8/17/2019 Lesson 5 - Data Structures

    43/63

    ni.com/training

    P"otting Data 

    se the Conte)t Be"* indo to determine ho to ire

    multi-plot data to 9a0eform :raphs) Charts and 5 :raphs.

  • 8/17/2019 Lesson 5 - Data Structures

    44/63

    ni.com/training

    (rror C"usters

    • 8a*=IE9 uses error clusters to pass error information.

    • An error cluster contains the folloing elements#

    6 statusBoolean 0alue that reports True if an error occurs.

    6 code&!-*it signed integer that identifies the error.

    6 source1tring that identifies here the error occurred.

  • 8/17/2019 Lesson 5 - Data Structures

    45/63

    9OAL

    ()ercise 5!#

    Tem*erature +arnings ' C"usters

    Create clusters) reorder clusters) and use the cluster functions

    to assem*le and disassem*le elements.

  • 8/17/2019 Lesson 5 - Data Structures

    46/63

    DISC-SSIO?

    ()ercise 5!#

    Tem*erature +arnings ' C"usters

    • 9hat ould happen if you reordered cluster elements>

    • 9hat ould happen if you added a cluster element to

    9eather Data @ut>

  • 8/17/2019 Lesson 5 - Data Structures

    47/63

    ni.com/training

    $. Ty*e Definitions

    Type Definitions and Custom Data Types

    Creating and Identifying Type Definitions

    Controls

    1trict Type Definitions

  • 8/17/2019 Lesson 5 - Data Structures

    48/63

    ni.com/training

    Ty*e Definitions 6Ty*e Def7

    • A type definition is a master copy of a custom data type%control) indicator) or constant'.

    6 A custom data type is sa0ed in a .ctl file.

    6 Instances of a type def are lin/ed to the .ctl file.

    • Instances can *e controls) indicators) or constants.

    • 9hen the type def changes) instances automatically update.

    6 Changes include data type changes) elements added)

    elements remo0ed) and adding items to an enum.

  • 8/17/2019 Lesson 5 - Data Structures

    49/63

    ni.com/training

    Creating Ty*e Definitions 6Ty*e Def7

    . 4ight-clic/ a control) indicator or constant and select

    :ae Ty*edef.

    . 4ight-clic/ the o*2ect again and select O*en Ty*e Def.

    !. Edit control) if needed.

    &. 1a0e control as a .ctl file.

  • 8/17/2019 Lesson 5 - Data Structures

    50/63

    ni.com/training

    Identifying Ty*e Definitions 6Ty*e Def7

    • 8oo/ for a glyph mar/ing the upper left corner of terminals

    and constants.

    • $o0er cursor o0er glyph to 0ie tip strip.

    • =ie Context $elp hile ho0ering cursor o0er terminal or

    constant.

  • 8/17/2019 Lesson 5 - Data Structures

    51/63

    ni.com/training

    Ot,er Contro" O*tions

    5ou can sa0e a custom control as#• Control

    • Type Definition

    1trict Type Definition

  • 8/17/2019 Lesson 5 - Data Structures

    52/63

    ni.com/training

    Contro"

    • Instances are not lin/ed to a.ctl file.

    • Each instance is an independent copy of the control.

    • sed to create controls that *eha0e li/e existing controls *ut

    loo/ different.

  • 8/17/2019 Lesson 5 - Data Structures

    53/63

    ni.com/training

    Strict Ty*e Definition

    • 1trict type definitions are similar to a type definition in that#

    6  All instances lin/ to .ctl file.

    6 9hen attri*utes or data types change) all instances update.

    • Examples# Changing a /no* to a dial) a round 8ED to a s7uare

    8ED) or a dou*le to an integer.• 1trict type definitions enforce e0ery aspect of a instance

    except la*el) description) and default 0alue.

    • se strict type definitions to ensure all front panel instances

    ha0e the same appearance.

    i

  • 8/17/2019 Lesson 5 - Data Structures

    54/63

    9OAL

    ()ercise 5!&

    +eat,er +arnings ' Ty*e Definition

    Create and modify a type-defined cluster control. se the

    type definition in a calling =I and a su*=I.

    i

  • 8/17/2019 Lesson 5 - Data Structures

    55/63

    DISC-SSIO?

    ()ercise 5!&

    +eat,er +arnings ' Ty*e Definition

    +o that the 9eather Data control is sa0ed as a typedefinition) ho many instances of the cluster ould need to *e

    updated to add ind speed data>

  • 8/17/2019 Lesson 5 - Data Structures

    56/63

    ni.com/training

    Summaryui2

    . 5ou can create an array of arrays.a' True

    *' False

  • 8/17/2019 Lesson 5 - Data Structures

    57/63

    ni.com/training

    Summaryui2 Answer

    . 5ou can create an array of arrays.a' True

    %7 $a"se

    ou cannot drag an array data ty*e into an array

    s,e"". BoweverE you can create two!dimensiona"

    arrays.

  • 8/17/2019 Lesson 5 - Data Structures

    58/63

    ni.com/training

    Summaryui2

    !. 9hat is the 0alue of the Iterations indicatorafter running this =I>

  • 8/17/2019 Lesson 5 - Data Structures

    59/63

    ni.com/training

    Summaryui2 Answer

    !. 9hat is the 0alue of the Iterations indicatorafter running this =I>

    8

  • 8/17/2019 Lesson 5 - Data Structures

    60/63

    ni.com/training

    Summaryui2

    &. 9hich of the folloing custom control settings defines thedata type of all instances of a control *ut allos for

    different colors and font styles>

    a' Control

    *' Type definition

    c' 1trict type definition

    d' Cluster control

  • 8/17/2019 Lesson 5 - Data Structures

    61/63

    ni.com/training

    Summaryui2 Answer

    &. 9hich of the folloing custom control settings defines thedata type of all instances of a control *ut allos for

    different colors and font styles>

    a' Control

    %7 Ty*e definition

    c' 1trict type definition

    d' Cluster control

  • 8/17/2019 Lesson 5 - Data Structures

    62/63

    ni.com/training

    Summaryui2

    3. 5ou ha0e input data representing a circle# Position) 5 Position)and 4adius. In the future) you might need to modify your data to

    include the color of the circle.

    9hat data structure should you use to represent the circle in your

    application>

    a' Three separate controls for the to positions and the radius

    *' A cluster containing all of the data

    c' A custom control containing a cluster

    d' A type definition containing a clustere' An array ith three elements 4

    6@E7

  • 8/17/2019 Lesson 5 - Data Structures

    63/63

    Summaryui2 Answer

    3. 5ou ha0e input data representing a circle# Position) 5 Position)and 4adius. In the future) you might need to modify your data to

    include the color of the circle.

    9hat data structure should you use to represent the circle in your

    application>

    a' Three separate controls for the to positions and the radius

    *' A cluster containing all of the data

    c' A custom control containing a cluster

    d7 A ty*e definition containing a c"ustere' An array ith three elements 4

    6@E7