Summary: The creation of this content was supported in some part by NSF grant 0538934.
Writing to File
Consider the function in Figure 1 where a set of numbers in a one-dimensional array represents the resulting noisy signal is to be written to a file. This section will outline the steps required to create files.
Create a new G program, right click in the G programming window and select File Dialog from the Functions >> Programming >> File I/O >> Advanced Functions menu. Drag and drop the File Dialog function onto the G programming window.
The Configure File Dialog dialog box automatically appears to configure the function. Accept the default configuration shown in Figure 3 to create a single file by clicking the OK button.
The resulting diagram after closing the configuration dialog box is shown in . Optionally, right click on File Dialog and select View As Icon from the pop-up menu. This will save some real estate in the G programming window.
From the Functions >> Programming >> File I/O menu select Open/Create File, Write Binary File and Close File functions.
Arrange the File I/O operations as shown in Figure 7.
Right click on the operation (0:open) terminal of the Open/Create File function (highlighted in Figure 8).
Select Create >> Constant from the pop-up menu.
Arrange the diagram to look as in Figure 10.
Click on the down arrow in the operation constant just created and select open or create from the pop-up menu.
The resulting updated operation constant value is shown in Figure 12.
Repeat the process to create a constant for the access (0:read/write) terminal (highlighted in Figure 13).
Set the constant to write-only. Re-arrange the block diagram to look like the diagram shown in Figure 14. At this point, the file is set to create a new file for writing.
Get the Noisy Signal function and wire its output data to the Data terminal of the Write to Binary File function.
Complete the diagram by connecting the Open, Write and Close file operations as shown in Figure 16.
When this G program is executed, the standard file dialog box appears. Name the file to be written signal.dat.
Once the program completes executing, the signal.dat file is created and located in the location indicated by the path selected.
Reading From Files
The signal.dat file created in the previous example will be used to read data from a file. As in the previous example, select the File Dialog, Open/Create File, Read from Binary File andClose File functions.
Create constants by right clicking on the operation (0:open) and access (0:read/write) terminals of the Open/Create File operation. Set the constants to open and read-onlyrespectively (see Figure 20 ).
Similar to creating arrays, drop an array constant in the G diagram, drop a numeric constant onto the array constant and set the data type representation to double. Wire this array constant to the data type terminal of the Read from Binary File function as shown in Figure 21 .
In the Front Panel window, drop a Waveform Graph.
With the data type specified, wire the data terminal of the Read from Binary File function to the Waveform Graph terminal as shown in Figure 23 .
Complete the program by wiring refnum and error terminals of the Open/Create File, Read from Binary File and Close File functions as shown in Figure 24 .
When this program is executed, a file dialog box appears. Select the signal.dat file and click OK.
The binary data in signal.dat is read and plotted in a Waveform Graph. The result is shown in Figure 26
No comments:
Post a Comment