LabVIEW for Dummiees
Section 2

Back to the Beginning


For, While, and Sequence Loops

These are very similar to the for and while loops encountered in your first computing class.
A For Loop performs an operation for a certain number of times, say ten. The big N in the upper left hand corner is the set number of times for the loop to execute.
A While Loop performs an operation while a certain condition is true, say the end of the file has not been reached. The little round arrow in the lower right hand corner is the condition, that is, to keep the loop executing or not. This has to be wired with a Boolean operator or the loop will execute only once, negating the purpose of making a loop. Usually uses a comparison, Boolean logic operator, or simple control.
The little blue boxed i in the lower left hand corner is the iteration number. It represents the number of times the loop has been executed. Needed if you want an operation to occur at a certain step.

A Sequence Loop, which looks like a piece of film, performs an operation after one has been completed, say after acquiring the data, analyze the data. To add a Sequence frame, right click on the frame and choose Add Frame After or Add Frame Before or Duplicate Frame. They will be used in sequential order according to the numbering scheme on the top center. Many times you will want the data from a previous frame to be used in a later sequence, Add Sequence Local, where you can attach any wire to be brought out in a later frame. An arrow will show the direction of data flow. If you move a sequence local, it will move the same way in the other frames. Little programming tip: If you are acquiring data aver a several minutes and need to enter some physical constants needed after acquisition is complete, place the control for the physical constants inside the later sequence, not outside the loop. This way while the data is acquiring, your data can be entered SAVING TIME! This works because LabVIEW hasn't started on your data yet.

You may notice some problems when an Array enters a For Loop. What happens is the array is auto-indexed, i.e. the array's value at the index of the iteration is used. If you don't like, click on the little black "tunnel", and choose Disable Indexing. The Loop can also be used to build an array, by indexing the values to the iteration number. Look at this for a comparison. As you can see, the while and for loops both have this feature. Once again:
Enable Indexing allows you to build arrays for output, or auto-index input arrays
Disable Indexing allows you to just take the values as they come in or out.
By placing a Formula Node in the For Loop, and array can be evaluated, the number of times to evaluate is the size of the array, and the array will be automatically rebuilt upon exiting.

In For and While Loops a Shift Register can be added to the loop by right clicking on the loop and selecting Add Shift Register. This is invaluable when you need to use data from a previous loop in order to calculate the next loop. By choosing Add Element, you can use the element from two iterations ago. For example, by adding two elements, you can use the data from the previous three iterations.


Arrays

Arrays are invaluable. Nearly every application will need them. You can define vectors (a 1-D array) or matrices (a 2-D array) or a general N dimensional array, like tensors. Although Arrays may resemble vectors and matricies, they are not the same thing. This is how the basics work. Same principals for subtraction and division.

Scalar + Scalar = Scalar; 1+1=2
Scalar + Array  = Array;  1+[1,2,3]=[2,3,4]
Array  + Array  = Array;  [1,2,3]+[4,5,6]=[5,7,9], but [1,2]+[3,4,5]=[4,6]
Scalar * Scalar = Scalar; 4*5=20
Scalar * Array  = Array;  4*[4,5,6]=[16,20,24]
Array  * Array  = Array;  [1,3,3]*[4,6,2]=[4,18,6], but [1,3]*[4,6,2]=[4,18]
As you can see, only the values for the smallest array are used.
Even if you are not using vectors and matrices, they can be used to store a lot of data as one unit. Would you rather try to keep track of a bunch of wires or one wire? Arrays are numbered or indexed 0,1,2,...,N-1, where N is the size of the array. Arrays can be used for anything, Numeric, Boolean, String, etc. Just remember to keep the same type in the array, that is, don't mix String and Numeric or Boolean. I have used most of the array operations and they are all important.
  1. Build array. First choose the build array element. Go to the bottom corner of the node and click and drag till it's doubled. The regualr mouse arrow will change to a little corner. Create two numbers, and wire them to each of the inputs, and create and indicator. Run and on the front panel you will see the numbers combined into a 1-D array. Try with Boolean values and strings.
  2. Initialize Array is invaluable in loops especially with a Shift Register. First connect an initializing value to the element data, usually zero. If you need an N-D array, enlarge it like the build array. Enter the dimension size(s) which is the length (and width) of the array. Create an indicator and see if it worked. A little note, you can't initialize a random array by connecting the random operator to the element data. It will be random, but the array will have the same value.
  3. Index Array allows you to find the value at an index. Simply connect the array and list what index you want. If you need a column in a matrix you can choose the Disable Indexing on the boxes in the lower left corner to make a 2-D matrix into a 1-D vector. Be careful when using, it can be a bit tricky, check with known values before you have rely on the program. See this on extracting a row/column from a matrix.
  4. Transpose 2D Array simply flips the 2-D array over. That is, the rows and columns are interchanged.
  5. Array Size simply tells the size of the array. If it is a 1-D array with 15 elements, it returns 15. A 2-D array with 10 by 5 elements returns [10,5]
  6. Interleave 1D Arrays - Takes two arrays of the same size and type and interleaves them. For example, interleaving the two arrays [1,2,3] and [0,6,9] would result [1,0,2,6,3,9]. Order is important. If the sizes are unequal, the size will be only twice the smallest array. Try it and see.
  7. Decimate Arrays - Undoes the Interleave Arrays operation.
  8. Array Max & Min - Finds the Max and Min values and the indexes.
  9. Sort 1D Array - Sorts the Array in Accending Order. Either String or Numeric.
  10. Add Array Elements & Multiply Array Elements - found under the Numeric pallet allow you to either find the sum or product of all the elements in an array.
NOTE: Don't use the array constant without defining the type first, it won't work. You must insert a type. That is, if you want a numeric, drag and release a numeric control inside. Same for a string, Boolean, etc. Also, if you are moving the array on the front panel and accidentally drag one of the cells out, you'll get an empty undefined array. Ctrl+Z to undo, or replace the array.
The Sort, Interleave, and Max/Min functions are avaliable. However, sometimes you may need to sort two 1-D arrays with respect to each other, add an array onto the end of the other, or find the two highest and two lowest numbers. Try making these VIs. This is how I did them.


Strings

Strings are needed whenever you are working with an alphanumeric sequence of characters. Most of the times you will be using strings is in file creation and reading. You may have discovered that you can write notes on the diagram by using the mouse cursor. This is not a string! It's only a note or label. A string must appear in Pink. If you're not sure, try connmecting a wire to it. Choose String Constant. Type something in. Create Indicator. RUN. What you typed will appear on the front panel. Not very impressive. Copy this sequence into the box after changing it to '\' Code Display:
"This\sis\sa\stest\sto\ssee\sthe\s\t\stab\sand\s\r\nend\s\sof\sline\sbackslash\scodes."
Run it and you should see this on the front panel:
"This is a test to see the 	 tab and 
end of line backslash codes."
If you can't figure this out, here are what the \ codes mean. A complete list can be found under Backslash (‘\’) Codes Display. Also there are the tab and end of line operators in the string box:
1. \s - space
2. \t - Tab
3. \r\n - EOL (end of line)
The others I really never used or are self explanatory.


File Creation

Whenever you perform an experiment you need to save the results for later study. LabVIEW allows you to save the data as simple text files (*.txt) which can be studied in Microsoft Excel, or another spreadsheet program. If you need to create a file for the VI to read such as a control file or data file, you can form the file in Excel and Just remember to save it as a Text (Tab Delimited) File. Before you start your data collection two things:These seem very simple and common sense, but many people forget.
There are Four File sub-VIs that you will use most of the time.
  1. Write to Spreadsheet File.vi
  2. Read from Spreadsheet File.vi
  3. Write Characters to File.vi
  4. Read Characters from File.vi
The other sub-VIs you may need for advanced work, but I never needed them.
Here are terms with which you need to be familiar with wire colors Here is a little trick I learned. Whenever I needed to extract data line by line from a file, I modified the Read From Text File.vi, by removing the pausing operation, the Match Pattern, and adding a control for file path. If you are unclear here is how is should look


Well by now I have hopefully explained the concept of wiring, numeric, loops, Boolean operators, Strings, File creation and reading. UPDATED! REAL LIFE EXAMPLE!!!
So here is your first QUIZ!!

Time and Dialog

When you have written your programs you sometimes need to inform others about errors when entering data. This is done by using a Dialog Box

Other times you may need to slow down the program. You may be thinking why? The reason is that sometimes, especially when acquiring data or tracing a pattern, the program works too fast and you don't need data acquired every millisecond. Once a second is enough. I use the Wait (ms) most of the time. Be sure to remember it is in milliseconds or 0.001 seconds. That would make half a second 500 milliseconds. Another useful node is the Get Date/Time String which gives the date and time as determined by the computer to the minute. Useful when storing and dating your data. If accuracy is to the second is needed, Get Date/Time In Seconds gives the number of seconds since 12:00 AM January 1, 1904, UT. Why 1904? Don't ask me. For most people that is useless. That is why the calendar with days, months, and years was invented. By connecting the Format Date/Time String to the previous node, the time is given as a string in a familiar notation. When I was writing this the result was: 08/12/99 03:17:35 PM . That is for the default case (%c). You may want to change it if you're European. Is it Y2K or 2004+ compliant? I don't know. We'll find out very soon.


Graphing

Once you have taken all of your data, you need an easy way to visualize it. Unless you are very unusual, you can't just look at an array of numbers and determine the result. The ability to Graph and gather data from those graphs are two of LabVIEW's most important attributes. To obtain the graphs, you need to go the front panels and right click to bring up the controls. You can choose from graphs or charts. Charts have a buffer, that is, they only hold so many points. Charts are useful when acquiring data. When studying graphs I usually choose graphs. There are three major types of graphs you will use.
  1. Waveform which only needs the Y component, i.e. uses a 1-D array. It graphs the array's data over time.
  2. X,Y Plot - Plots X vs. Y The X-Y plot is a Cluster consisting of Bundled Elements. This is a bit difficult to explain. So I suggest you look at this picture. Try reproducing it.
  3. Intensity - Plots the values of a matrix, i.e. uses a 2-D array. Kind of like making a picture.
Also by using a Waveform Graph and a Bundle with three elements, you can create a graph with an offset and a set distance between points. All you will need is an array representing the Y values. On the bundle the first value is the offset, i.e. where to start; the second value is the "dx", i.e. the horizontal space bewteen the points; and third your array. Try it.

To make the graphs more user friendly, there are ways to interact with the graphs to extract data, label, zoom, interpolate, and color. By right clicking and choosing Show >>
  1. Label & Caption are basically the same thing allowing you to name your graph
  2. Legend is all important for your display of data. You can choose
  3. Pallet - Allows you to zoom, set the precision and expression of the axes, or to auto-scale
  4. Cursor Display - Move a cursor around the graph to study the plot. Lock to the plot, to point or allow to drag (the padlock). The little rhombus on the right allows you to make minor adjustments either up, down, right, or left by clicking. Only on appears on graphs. By using attribute nodes you record certain points. Coming later
  5. X-Scale & Y-Scale - Show the X or Y scale
  6. Scrollbar - Allows you to scroll around the graph in the x direction. Only appears on Charts.
  7. Chart History Length - Charts have a buffer size (default 1024 data points) so you can input numbers and they will be displayed as a graph. You can change it, but don't be ridiculous, the buffer is usually sufficient.

You can also change the graph ranges by clicking on the upper and lower bounds and replacing the numbers. If you want a logarithmic plot, you can either insert a Logarithm Base 10, or choose X Scale, Y Scale >> Formatting... to change the number to log, also once there you can change the digits of precision, the notation, and if you want graph lines.


Continue to Section 3