Saturday, September 25, 2010

Input and Output

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.
Figure 1: Noisy Signal Function
A screen capture of a noisy Signal function. There are three separate boxes comprised in this screen capture. From left to right, there is a small blue box containing the number 100. Followed bt a large box containing a diagram of the function, and then the last box is labeled the signal and it is an orange box.
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.
Figure 2: File Dialog
A screencap of a file dialog. There is a level listing at the top. The level nesting is as follows, with upper most level first. Programming, them File I/0, and then Advanced File Functions. Below Advanced File Functions is a horizontal list of four icons. From left to right the icons are labeled: Get file position, get file size, set Type &C... and finally, File Dialog. The File Dialog icon is contained within a light blue box.
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.
Figure 3: Configure File Dialog
A typical Windows window. It is labeled 'Configure File Dialog [File Dialog]'. Contained in the window is a form with the top option selected 'Limit Selection to single item' below that the options 'File' and 'New or existing'. At the bottom of the form there are the buttons, 'ok', 'concel' and 'Help'.
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.
Figure 4: G File Dialog
A screencap of a g file dialog. There is a 'selected path' option at the bottom of the icon.
Figure 5: View As Icon
A screencap of a menu. From top to bottom the menu options are 'Visible Items', 'Open Front Panel', 'Size To Text', 'View As Icon' and 'Properties'. 'View As Icon' is highlighted blue.
From the Functions >> Programming >> File I/O menu select Open/Create File, Write Binary File and Close File functions.
Figure 6: File Input and Output Operators
A screencap of 'File Input and Output Operators'. There are two main parts to this image. At the top there is a file listing with top level as 'Programming' and then 'File I/O'. The other part of the diagram is located below this file listing and is an array of three rows of icons with 7 icons on the first two row and 6 on the bottom row.
Arrange the File I/O operations as shown in Figure 7.
Figure 7: Open, Write and Close File Diagram
A row of 4 icons. The icon furthest to the left is highlighted in blue and labeled 'File Dialog'.
Right click on the operation (0:open) terminal of the Open/Create File function (highlighted in Figure 8).
Figure 8: File Create Operation
A row of 4 icons. The icon furthest to the left is highlighted in blue and labeled 'File Dialog'. The next icon is labeled 'operation (0:open)'.
Select Create >> Constant from the pop-up menu.
Figure 9: Create Operation Constant
A screencap of a typical menu. The first menu has the item 'Create' selected and then points a nested menu with the item 'Constant' highlighted.
Arrange the diagram to look as in Figure 10.
Figure 10: Operation Constant
A row of four icons. The first icon is highlighted blue and labeled 'File Dialog'. Underneath the first icon is a blue box with the word 'open' contained in it. A line connects that blue box to the second icon.
Click on the down arrow in the operation constant just created and select open or create from the pop-up menu.
Figure 11: Open or Create File Operation
A screencap of a menu. The first item is 'open' and it has a check mark next to it. The fourth item is 'open and create' and it is highlighted blue.
The resulting updated operation constant value is shown in Figure 12.
Figure 12: Create File to Write
A row of four icons. The first icon is highlighted blue and labeled 'File Dialog'. Underneath the first icon is a blue box with the word 'open or create' contained in it. A line connects that blue box to the second icon.
Repeat the process to create a constant for the access (0:read/write) terminal (highlighted in Figure 13).
Figure 13: File Access Mode
A row of four icons. The first icon is highlighted blue and labeled 'File Dialog'. Underneath the first icon is a blue box with the word 'access (0:read/write)' contained in it. A line connects that blue box to the second icon. The second icon is colored with little diamonds of different colors along the border of the icon.
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.
Figure 14: Write Only Mode
A row of four icons. The first icon is highlighted blue and labeled 'File Dialog'. Underneath the first icon is a blue box with the word 'open or create' contained in it. Underneath the first icon is a blue box with the word 'write-only' contained in it. A line connects each of these blue boxes to the second icon.
Get the Noisy Signal function and wire its output data to the Data terminal of the Write to Binary File function.
Figure 15: Writing Binary Data
A row of four icons. The first icon is highlighted blue and labeled 'File Dialog'. Underneath the first icon is a blue box with the word 'open or create' contained in it. Underneath the first icon is a blue box with the word 'write-only' contained in it. A line connects each of these blue boxes to the second icon. Below the second icon there is a box containing the phrase 'Noisy Signal' and A golden line connects this box to the third icon.
Complete the diagram by connecting the OpenWrite and Close file operations as shown in Figure 16.
Figure 16: Writing to File G Program
A row of four icons. The first icon is highlighted blue and labeled 'File Dialog'. Underneath the first icon is a blue box with the word 'open or create' contained in it. Underneath the first icon is a blue box with the word 'write-only' contained in it. A line connects each of these blue boxes to the second icon. Below the second icon there is a box containing the phrase 'Noisy Signal' and A golden line connects this box to the third icon. A light blue squiggly line connects the first icon to the second icon. Straight light blue lines and yellow squiggly lines connect the second icon to the third icon and the third icon to the fourth icon.
When this G program is executed, the standard file dialog box appears. Name the file to be written signal.dat.
Figure 17: Create File Dialog Box
A screencap of a Windows window. It shows a create file dialog window.
Once the program completes executing, the signal.dat file is created and located in the location indicated by the path selected.
Figure 18: Data File signal.dat
A screencap of a Windows window. It shows the location of Data File signal.dat.

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 DialogOpen/Create FileRead from Binary File andClose File functions.
Figure 19: Operators to Read Files
A row of four icons. The First icon is contained within a light blue box and is labeled 'File Dialog'.
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 ).
Figure 20: Set to Open and Read-Only
A row of four icons. The First icon is contained within a light blue box and is labeled 'File Dialog'. Below the first icon are two blue boxes the upper box contains the word 'open'and the bottom bos is labeled 'read-only. There are blues lines connecting these boxes to the second icon. A light blue squiggly line connects the first icon to the second.
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 .
Figure 21: Data Type to Read
A row of four icons. The First icon is contained within a light blue box and is labeled 'File Dialog'. Below the first icon are two blue boxes the upper box contains the word 'open'and the bottom bos is labeled 'read-only. There are blues lines connecting these boxes to the second icon. A light blue squiggly line connects the fist icon to the second. A golden icon comprised of two rectangles is about icons 2 and 3 with a golden line connecting it to the 3rd icon.
In the Front Panel window, drop a Waveform Graph.
Figure 22: Graph for Data to be Read
A screencap of a graph. Time is labeled from 0 to 100 on the x axis, and amplitude is labeled from -1.5 to 1.5 on the y-axis.
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 .
Figure 23: Data to be Read
A row of four icons. The First icon is contained within a light blue box and is labeled 'File Dialog'. Below the first icon are two blue boxes the upper box contains the word 'open'and the bottom bos is labeled 'read-only. There are blues lines connecting these boxes to the second icon. A light blue squiggly line connects the fist icon to the second. A golden icon comprised of two rectangles is about icons 2 and 3 with a golden line connecting it to the 3rd icon. There is another golden line connecting the third icon to a little graph icon over the fourth icon.
Complete the program by wiring refnum and error terminals of the Open/Create FileRead from Binary File and Close File functions as shown in Figure 24 .
Figure 24: Read Binary Data G Program
A row of four icons. The First icon is contained within a light blue box and is labeled 'File Dialog'. Below the first icon are two blue boxes the upper box contains the word 'open'and the bottom bos is labeled 'read-only. There are blues lines connecting these boxes to the second icon. A light blue squiggly line connects the fist icon to the second. A golden icon comprised of two rectangles is about icons 2 and 3 with a golden line connecting it to the 3rd icon. There is another golden line connecting the third icon to a little graph icon over the fourth icon. There are two yellow squiggly lines connection the second icon to the third and the third to the fourth.
When this program is executed, a file dialog box appears. Select the signal.dat file and click OK.
Figure 25: Select Binary File to Read From
A screencap of a Windows window. It shows how to select a binary file to read from window.
The binary data in signal.dat is read and plotted in a Waveform Graph. The result is shown in Figure 26
Figure 26: Read Data Graphed
A screencap of a graph. Time is labeled from 0 to 100 on the x axis, and amplitude is labeled from -1.5 to 1.5 on the y-axis. Above the graph is a little square that says 'Plot 0'. The graph contains a jagged sin wav.

Content actions

GIVE FEEDBACK:

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Popular Projects

My Blog List

Give support

Give support
Encourage Me through Comments & Followers

Followers