Skip to content Skip to navigation Skip to collection information

OpenStax-CNX

You are here: Home » Content » Introduction to G Programming » Input and Output

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • NSF Partnership display tagshide tags

    This collection is included inLens: NSF Partnership in Signal Processing
    By: Sidney Burrus

    Click the "NSF Partnership" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Featured Content display tagshide tags

    This collection is included inLens: Connexions Featured Content
    By: Connexions

    Click the "Featured Content" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

Input and Output

Module by: Eduardo Perez. E-mail the author

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 Open, Write 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 Dialog, Open/Create File, Read from Binary File and Close 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-only respectively (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 File, Read 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.

Collection Navigation

Content actions

Download:

Collection as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Module as:

PDF | More downloads ...

Add:

Collection to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks

Module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks