File InputOutput


File Input/Output

File input and output (I/O) operations retrieve information from and store information in a disk file. LabVIEW has a number of very versatile file I/O functions, as well as some simple functions that take care of almost all aspects of file I/O in one shot. We'll talk about the simple file functions in this chapter. All are located in the Programming>>File I/O subpalette of the Functions palette.

How They Work

The File functions expect a file path input, which looks kind of like a string. A path is a specific data type that provides a platform-specific way to enter a path to a file. We talked about them briefly in Chapter 4, and they'll come up again in Chapter 14, "Advanced LabVIEW Data Concepts." If you don't wire a file path, the File functions will pop up a dialog box asking you to select or enter a filename. When called, the File functions open or create a file, read or write the data, and then close the file. The files created with the VIs we'll talk about now are just ordinary text files. Once you have written data to a file, you can open the file using any word processing program to see your data.

One very common application for saving data to file is to format the text file so that you can open it in a spreadsheet program. In most spreadsheets, tabs separate columns and EOL (end of line) characters separate rows.

Express Writing and Reading of Measurement Files

For quick and interactive configuration of data file I/O operations in LabVIEW, consider using the Write To Measurement File and Read From Measurement File Express VIs, shown in Figure 9.29 and Figure 9.30.

Figure 9.29. Write To Measurement File


Figure 9.30. Read From Measurement File


These VI pass data as the dynamic data type (which we learned about in Chapter 8, "LabVIEW's Exciting Visual Displays: Charts and Graphs"), so they can handle just about any type of file containing measurement data.

To configure these Express VIs after they are placed on the block diagram, open the configuration dialog by double-clicking on the subVI or selecting Properties from the pop-up menu.

From the Write To Measurement File Express VI configuration dialog (shown in Figure 9.31), you can specify how a file will be formatted, and what data to store in the file.

Figure 9.31. Write To Measurement File configuration dialog


From the Read From Measurement File Express VI configuration dialog (shown in Figure 9.32), you can specify the format of the file to be read. If you press the Read File Now button, LabVIEW will read the file once and populate the Sample Data table to show you whether the read operation worked successfully.

Figure 9.32. Read From Measurement File configuration dialog


Writing and Reading Spreadsheet Files

If the Express VIs do not provide you with enough flexibility, you can read and write directly to spreadsheet text files with Write To Spreadsheet File and Read From Spreadsheet File.

Write To Spreadsheet File converts a 2D or 1D array of single-precision numbers to a text string, and then writes the string to a new byte stream file or appends the string to an existing file. You can optionally transpose the data. Do not wire inputs for both 1D and 2D data (or one will be ignored). The text files created by this VI are readable by most spreadsheet applications.

Figure 9.33. Write To Spreadsheet File.vi


Read From Spreadsheet File reads a specified number of lines or rows from a numeric text file, beginning at a specified character offset, and converts the data to a 2D single-precision array of numbers (see Figure 9.34). You can optionally transpose the array. This VI will read spreadsheet files saved in text format.

Figure 9.34. Read From Spreadsheet File.vi.


Write To Spreadsheet File.vi and Read From Spreadsheet File.vi do not have Error In or Error Out terminals. They do all of their error handling internally, and will open an error dialog if an error does occur inside the VI. This behavior can be nice for simple applications, but is generally undesirable for advanced applications.


Spreadsheet Files: CSV Versus XLSWhat's the Difference?

Write To Spreadsheet File.vi and Read From Spreadsheet File.vi write and read data as text files using a delimiter (usually a comma or tab) to separate columns and an end of line character (\r, \n, or \r\n) to separate rows. Commonly, a spreadsheet file that uses commas as the delimiter is referred to as a Comma Separated Values (CSV) file and is routinely saved with the filename extension ".csv," which Microsoft Excel, OpenOffice.org Calc, and other spreadsheet applications will recognize. For example, on a computer with Microsoft Excel installed, you can double-click on a CSV file and it will open in Excel. In fact, CSV files (if named with a ".csv" file extension) will have an icon, very similar to Excel (".xls") files, as shown in Figure 9.35.

Figure 9.35. Excel icons for XLS and CSV files


However, it is important to realize that if you save a file in Excel (or another spreadsheet program), it will not save the file as a CSV (unless you specify that option); but rather, it will be saved as a binary file (usually with a different file extension, such as ".xls" for Excel files). The resulting binary file cannot be read using Read From Spreadsheet File.vi.

The bottom line is that users of your application may say that they want data stored in Excel files, but what they really mean is that they want data stored in a file that Excel can read. Generate CSV files for your users, and they will probably be very happy with the resultand sometimes they won't even know the difference.

Activity 9-3: Writing to a Spreadsheet File

You will modify an existing VI to save data to a new file in ASCII format. Later you can access this file from a spreadsheet application.

1.

Open Graph Sine Array.vi, which you built in Chapter 8. If you didn't finish building that VI, you can find a finished version in EVERYONE\CH08. As you recall, this VI generates two data arrays and plots them on a graph. You will modify this VI to write the two arrays to a file in which each column contains a data array.

2.

Open the diagram of Graph Sine Array.vi and modify the VI by adding the diagram code shown inside the oval, as shown in Figure 9.36.

Figure 9.36. Graph Sine Array.vi block diagram, with modifications you will perform during this activity


Write to Spreadsheet File VI

The Write To Spreadsheet File VI (Programming>>File I/O palette) converts the 2D array to a spreadsheet string and writes it to a file. If no path name is specified (as in this activity), then a file dialog box will pop up and prompt you for a file name.

Boolean Constant

The Boolean Constant (Programming>>Boolean palette) controls whether or not the 2D array is transposed before it is written to file. To change it to TRUE, click on the constant with the Operating tool. In this case, you do want the data transposed because the data arrays are row specific (each row of the 2D array is a data array). Because you want each column of the spreadsheet file to contain data for one waveform, the 2D array must first be transposed.

3.

Return to the front panel and run the VI. After the data arrays have been generated, a file dialog box will prompt you for the file name of the new file you are creating. Type in a file name (or if you don't see this option, click on the "New . . ." button from the dialog box, and choose "File") and click the OK button. Remember the name and location of the file, as you will read in the data in the next exercise.

Do not attempt to write data files in VI libraries with the file I/0 VIs. Doing so may overwrite your library and destroy your previous work.

4.

Save the VI in your MYWORK directory, name it Graph Sine Array to File.vi, and close the VI.

5.

Use spreadsheet software if you have it, or a simple text editor, to open and view the file you just created. You should see two columns of 100 elements each.

Activity 9-4: Reading from the Spreadsheet File

You will write a VI to read in the data from the file written in the last exercise and plot it on a graph.

1.

Open a new VI and place a waveform graph on its front panel. Make sure autoscaling is on.

2.

Create the little block diagram shown in Figure 9.37. Use the Read From Spreadsheet File function to bring in data and display it on the graph.

Figure 9.37. Front panel and block diagram of the VI you will create during this activity


3.

Using the TRUE Boolean Constant, you must transpose the array when you read it in, because graphs plot data by row and it has been stored in the file by column. Note that if you hadn't transposed the data in the last exercise to store it in columns in the file, you wouldn't have to transpose it back now.

4.

Run the VI. Because you are not providing a file path, a dialog box will prompt you to enter a filename. Select the file you created in Activity 9-3.

The VI will read the data from the file and plot both waveforms on the graph.

5.

Save the VI in your MYWORK directory as Read File.vi.

More Writing and Reading of Files

You've seen how to read and write spreadsheet files or measurement data (using the dynamic data type). These functions are very easy to use.

Now we will discuss basic file I/O functions for working with text and binary files.

Writing and Reading Text Files

If you need to read or write a plain text file, you can use the LabVIEW functions on the Programming>>File I/O palette Write To Text File and Read From Text File.

Use the Write To Text File (shown in Figure 9.38) to write a string of characters to a file. Use Read From Text File (shown in Figure 9.39) to read a string of characters from a file.

Figure 9.38. Write To Text File


Figure 9.39. Read From Text File


The file input of Write To Text File and Read From Text File are path data types. However, you can wire a file refnum (which is discussed in Chapter 14, "Advanced LabVIEW Data Concepts") instead and the file input will adapt to a refnum type. These VIs are smart, in the sense that they can accept either a path or a refnum as their input.

It is important to note, that when you wire a path input into these VIs, if you do not connect a wire to the refnum out terminal, then you do not need to close the file refnum using the Close File function (which is discussed in Chapter 14). Write To Text File and Read From Text File are smart and will automatically close the file, if you do not wire anything to the refnum out terminal. The same is true for Write To Binary File and Read From Binary File


When you call Write To Text File and Read From Text File repeatedly (in a While Loop, for example), they will "remember" where they left off. For the Write To Text File function, this means that each time you write a string of characters, they are appended to the last string of characters that you wrote. For the Read From Text File function, this means that when you read some number of characters (specified by the count input), it will read that number of characters starting where you left off, last time you read the file. LabVIEW keeps track of where you left off automatically, storing this position in the file marker, to make your life easier. As we will learn next, we can explicitly set and get the position of the file marker, which makes it possible to have random access to data stored in large files.

Writing and Reading Binary Files

Just as you can read and write plain text files, LabVIEW also gives you functions to read and write binary files.

Binary files are efficient for storing large amounts of non-text data because, generally, they take up much less disk space than text files. For example, storing the number "3.14159265358979" in a binary file might only take up eight bytes (as a double-precision number), whereas in a text file it would take up 16 bytes (one byte for each character).

On the other hand, you can't easily open binary files with another application (like Word or Excel) because those other applications don't "know" how the data was formatted. Because a binary file could contain any type of data (for example, an array of integers, or a cluster of strings), it's up to you to specify how to read back that data. When you write a binary file with LabVIEW, you'll generally use LabVIEW to read back that binary file.

The Write To Binary File function is very easy to use and works just like the Write To Text File function we saw earlier, except that you can wire any kind of data you like to the data input (see Figure 9.40). The file input can be either a path or a file refnum.

Figure 9.40. Write To Binary File


The Read From Binary File is also easy to use, and is similar to the Read From Text File function, except that you must tell Read From Binary File what kind of binary data is stored in the file you are trying to read (see Figure 9.41). You do this by wiring the right kind of data to the data type input. You must wire the same data type that you used to write the file. Otherwise, you'll get garbage results in the data output.

Figure 9.41. Read From Binary File


For example, if you wrote arrays of Booleans to your binary data file, then you need to wire an array of Booleans to the data type input.

Activity 9-5: Reading a Text File

You will create a VI that reads the entire contents of a text file and displays the contents in a string indicator and the file size in a numeric indicator.

1.

Build the front panel shown in Figure 9.42.

The VI will read the contents of the file specified by the File Path control and display the output in the string indicator. The digital indicator will display the file's length.

Figure 9.42. Front panel of the VI you will create during this activity


2.

Build the block diagram in Figure 9.43.

Figure 9.43. Block diagram of the VI you will create during this activity


Read from Text File Function

Read From Text File function (Programming>>File I/O palette) returns the contents of a text file in a string.

Simple Error Handler.vi

Simple Error Handler.vi (Programming>>Dialog & User Interface palette) displays a dialog if there is an error in one of the File I/O functions.

3.

Return to the front panel and enter a path to a text file into the file path control. Make sure that you don't select a file that is too large (which will cause LabVIEW to use a lot of memory), or is a binary file (which will just display funny characters).

4.

Run the VI and look at the file contents that are displayed in the string indicator, and the file size that is displayed in the numeric indicator.

5.

Save and close the VI. Name it Read Text File.vi and place it in your MYWORK directory. Great jobyou're just made a text file viewer!

Activity 9-6: Writing and Reading Binary Files

As you saw in the previous activity, reading a text file is pretty trivial. In this activity, you'll build a VI that writes to a binary file and then another VI that can read those binary files.

1.

First, let's create the VI that writes a binary file. Build a front panel like the one shown in Figure 9.44, with one chart and one array of numbers.

Figure 9.44. Write To Binary File front panel


2.

Build a block diagram like the one shown in Figure 9.45.

Figure 9.45. Write To Binary File block diagram


We'll make our binary data by generating an array of points that represents sinewave. To do this, we can use a function called Sine Wave.vi. You can find this function in the Programming>>Signal Processing>>Signal Generation palette, as shown in Figure 9.45.

3.

Save your VI as Write To Binary File.vi. Run your VI to test it. You should see the data plotted, and you will be prompted to save to a file. You call your file "My Data.dat" or something similar.

Sine Wave.vi (found on the Programming>>Signal Processing>>Signal Generation palette) generates an array containing a sinewave (see Figure 9.46).

Figure 9.46. Sine Wave.vi


4.

Now let's build the VI that can read the binary file you just created. Create a front panel and block diagram like the ones shown in Figures 9.47 and 9.48.

Figure 9.47. Read From Binary File.vi front panel


Figure 9.48. Read From Binary File.vi block diagram


5.

For the block diagram, use the Read From Binary File function from the Programming>>File IO palette.

Notice that you MUST wire the "data type" input. Unlike text files, when you read a binary file, you have to specify what kind of data is inside the file. Because we know that this file was written as an array of DBL, we create an array constant on the block diagram of DBL.

To create the array constant "data type: array of DBL," you do the following:

a. From the Programming>>Array palette, choose Array Constant and place the blank array constant on the block diagram.

b. From the Programming>>Numeric palette, choose Numeric Constant and place this inside the array box.

c. You have an array now, but the default numeric datatype is an integer (I32). Right-click on the numeric constant inside the array and select Representation>>DBL.

d. Now wire this array constant to the "data type" input on the Read From Binary File.

6.

Save your VI as Read From Binary File.vi and run it. When prompted, select the binary file you saved earlier.

7.

As an interesting exercise, use the Read From Text File.vi you created and open the binary file.




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