LabVIEW for Dummiees
Section 2
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 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.
- 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.
- 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.
- 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.
-
Transpose 2D Array simply flips the 2-D array over. That is, the rows and columns
are interchanged.
- 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]
- 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.
- Decimate Arrays - Undoes the Interleave Arrays operation.
- Array Max & Min - Finds the Max and Min values and the indexes.
- Sort 1D Array - Sorts the Array in Accending Order. Either String or
Numeric.
- 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 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)
- Concatenate Strings is used to combine multiple strings and can be expanded in
the same way as an array. If you need a delimiter between strings, be sure to include it in the Concatenate Strings.
Very useful when need to combine a descriptive string with an array.
- Match Pattern - is very useful when extracting an alphanumeric character string
from a file, such as contains: name \s 10 20 30 .
1. name \s 10 20 30 is the String
2. \s is the Regular Expression
3. name is the Before Substring
4. \s is the Match Substring
5. 10 20 30 is After Substring
6. The Offset is where to start.
- Format Into String - takes a number and converts it into a string. You can't
just wire a number with a string. Also, you need to set the format string.
- Format - determines to what precision you want the
number represented. Even if it is only an integer, there are the invisible zeros. These are some you may need:
%.3f
which is a 3 decimal floating point number. This is the default when saving files.
%.0f - an integer
%.5e - a floating point number to 5 decimal places in scientific notation
If you need others
look in the String Syntax Elements Table >> Conversion Codes
- Array to Spreadsheet String - Works like the Format Into String except you need
to insert a delimiter, but the default is Tab. A problem you may encounter is the need to include the data from several
arrays on one line in a file. However, the array is converted with an end of line break. The way to remove this is by
using the Match Pattern, using the EOL character as the Regular Expression,
and the data is contained in the Before Substring. Then simply use Concatenate
Strings to combine the strings. Be sure to include Tabs between the strings and an EOL at the end.
- Spreadsheet String to Array - Transforms a numeric delimited string into an
array. I use the way it appears in my altered Read from Text File.vi.
The others I really never used or are self explanatory.
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:- Have all the
necessary data included in your file(s)
- Know where all the data is being placed
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.- Write to Spreadsheet File.vi
- Read from Spreadsheet File.vi
- Write Characters to
File.vi
- 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- FILE PATH -
The location of the file you want to read or write, such as C:\Windows\Desktop\myfile.txt
If you are working with a large number of files that need to combine strings to make a file name, you need to use the
String to File Path
- DELIMITER - The special pattern between the data entries, defaulted to Tab, no
real reason the change unless you are using another program to read the file that needs another delimiter. For example,
Mathematica needs comma delimited files.
- APPEND TO FILE - Put new data in the
file, defaulted
to False. Change to True especially if you are acquiring data over a period of time
- CHARACTER STRING - The data has been converted to an alphanumeric sequence, needed if data
contains words and numbers.
- EOF (End of File) If you are reading the lines
from a file, in a loop you will need to know when the end has been reached. NOTE: If this is in a loop you will need
the Boolean NOT to keep the loop operating.
- Format - See under strings
- 1D Data
and 2D Data - are used when you are using Array data in vector or matrix form. NOTE: If you are extracting
the data with a mixture of types, numeric and symbolic, only the numeric will be extracted correctly.
- TRANSPOSE - flip the array over. Use when 1-D array very large (>256) or you
just want a column vector. Useful when you are going to analyze the data later in a spreadsheet.
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!!
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.
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.- Waveform which only needs the Y component, i.e. uses a 1-D array. It graphs the
array's data over time.
- 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.
- 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 >>
- Label & Caption are basically the same thing allowing you to name your
graph
- Legend is all important for your display of data. You can choose
- Common Plots - Waveform, points, points and line, bar graph, base fill. I used the default usually
- Point Style, Line Style, Line Width, & Bar Plots, I think are self explanatory
- Fill Base Line - If you want to fill the region under the graph. I never used it.
- Interpolation - Only points, or connect the dots either as a square wave or as a even wave
- Color - Change the plots color. Very useful when examining more than one data set at a time
- X-Scale & Y-Scale I never did anything with it
- Pallet - Allows you to zoom, set the precision and expression of the axes, or to
auto-scale
- 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
- X-Scale & Y-Scale - Show the X or Y scale
- Scrollbar - Allows you to scroll around the graph in the x direction. Only appears on
Charts.
- 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.