Using Auto-Indexing


The For Loop and the While Loop can index and accumulate arrays at their boundaries automaticallyone new element for each loop iteration. This capability is called auto-indexing.

One important thing to remember is that auto-indexing is enabled by default on For Loops but disabled by default on While Loops.

Figure 7.7 shows a For Loop auto-indexing an array at its boundary. Each iteration creates the next array element. After the loop completes, the array passes out of the loop to the indicator; none of the array data is available until after the loop finishes. Notice that the wire becomes thicker as it changes to an array wire type at the loop border.

Figure 7.7. A For Loop auto-indexing an array at is boundary


If you need to wire a scalar value out of a For Loop without creating an array, you must disable auto-indexing by popping up on the tunnel (the square with [ ] symbol) and choosing Disable Indexing from the tunnel's pop-up menu (see Figure 7.8).

Figure 7.8. Choosing Disable Indexing on a tunnel's pop-up menu


In Figure 7.9, auto-indexing is disabled, and only the last value returned from the Random Number (0-1) function passes out of the loop. Notice that the wire remains the same size after it leaves the loop. Pay attention to this wire size, because auto-indexing is a common source of problems among beginners. They often create arrays when they don't want to, or don't create them when they need them, and then go crazy trying to figure out why they get a bad wire.

Figure 7.9. A loop output tunnel with auto-indexing disabled


Auto-indexing also applies when you are wiring arrays into loops. If indexing is enabled as in loop (A), the loop will index off one element from the array each time it iterates (note how the wire becomes thinner as it enters the loop). If indexing is disabled as in loop (B), the entire array passes into the loop at once (see Figure 7.10).

Figure 7.10. An input tunnel with auto-indexing enabled (A) and disabled (B)


Because For Loops are often used to process arrays, LabVIEW enables auto-indexing by default when you wire an array into or out of them. By default, LabVIEW does NOT enable auto-indexing for While Loops. You must pop up on the array tunnel and choose Enable Indexing from the pop-up menu if you want your While Loop to auto-index. Pay close attention to the state of your indexing, lest you develop errors that are tricky to spot.


Using Auto-Indexing to Set the For Loop Count

When you enable auto-indexing on an array entering a For Loop, LabVIEW automatically sets the count to the array size, thus eliminating the need to wire a value to the count terminal. If you give LabVIEW conflicting countsfor example, by setting the count explicitly and by auto-indexing (or by auto-indexing two different size arrays)LabVIEW sets the count to the smallest of the choices. In Figure 7.11, the array size, and not the value wired to the count terminal, determines the number of For Loop iterations, because the array size is the smaller of the two.

Figure 7.11. A For Loop that will execute 10 times





LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net