Skip to content Skip to navigation Skip to collection information

Connexions

You are here: Home » Content » Introduction to G Programming » Introduction to G Programming

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.
 

Introduction to G Programming

Module by: Eduardo Perez. E-mail the author

Summary: The creation of this content was supported in some part by NSF grant 0538934.

G is a high level, data-flow graphical programming language designed to develop applications that are

  • Interactive
  • Execute in Parallel
  • Multicore

The program is a block diagram edited in the Block Diagram programming window.

Figure 1: G Block Diagram
A flow chart showing connections and movement between items like graphs, dice, arrows containing a plus-sign, and other objects in a G-block.

The program input data and results are manipulated and displayed in the Front Panel window.

Figure 2: G User Interface
A screen capture of the G user interface with a sinusoidal graph plotted on a horizontal axis, labeled time, and a vertical axis, amplitude.

Hello Graphical World

The first program is to display the text “Hello graphical interactive parallel multicore world” in the Front Panel window.

Right click on the Block Diagram window and select String Constant from the Functions >> Programming >> String menu.

Drag and drop the String Constant onto the Block Diagram window as show in Figure 3.

Figure 3: String Constant
A screen capture of a window titled functions, with buttons to search and view, a hierarchical list beginning with Programming, then String, and two objects, labeled string constant and empty string.

Type in “Hello graphical interactive parallel multicore world.” in the String Constant.

Figure 4: "Hello…world" String Constant
A string constant that reads, Hello graphical interactive parallel multicore world.

Right click in the Front Panel window and select a String Indicator from the Controls >> Modern >> String & Path menu.

Figure 5: Select String Indicator
A screen capture of a window titled controls, with buttons to search and view, a hierarchical list beginning with Modern, then String and Path, and two objects, labeled string control and string indicator.

Drop it into the Front Panel window.

Figure 6: String Indicator
A beveled rectangle inside a gridded box labeled string.

Return to the programming window. Notice the string terminal corresponding to the string indicator in the Front Panel window. As you approach the string constant from the right, the wiring terminal is highlighted and the pointer turns to wire spooler.

Figure 7: Wiring the G Diagram
A pink box containing the caption, hello graphical interactive parallel multicore world. To the right of the pink box is another pink box labeled string, containing a shaded grey rectangle containing the letters a, b, c. The boxes are not connected, but an object that resembles a spool of wire is connected to the box on the left.

Click the “Hello graphical interactive parallel multicore world” terminal and then click on the String Indicator triangular terminal to wire the terminals.

Figure 8: Wired G Block Diagram
A pink box containing the caption, hello graphical interactive parallel multicore world. To the right of the pink box is another pink box labeled string, containing a shaded grey rectangle containing the letters a, b, c. The boxes are connected by a zig-zag pink line.

Save your program as Hello, World.vi. Return to the Front Panel window. Click the run button (➯). You have successfully completed and executed your first G program.

Figure 9: Hello, World G Program Executed
A grey grid labeled String containing a grey rectangle with a caption that reads, hello graphical interactive parallel multicore world.

Arithmetic Expressions

The next program converts degrees from Fahrenheit to Celsius using the formula

C=59(F32)C=59(F32) size 12{C= { {5} over {9} } \( F - "32" \) } {}.

In the Block Diagram window, select the subtract, multiply and divide from the Functions >> Mathematics >> Numeric menu

Figure 10: Numeric Operations
A screen capture of a window containing a hierarchical list, beginning with Mathematics, and below it, numeric. Below the list are four objects, which are yellow triangles containing operators, and they are labeled add, subtract, multiply, and divide.

Wire the subtract, multiply and divide functions as shown in Figure 11.

Figure 11: Subtract, Multiply and Divide
A diagram connecting three yellow triangles containing operations together. Subtract is connected to Multiply, and multiply is connected to divide.

Right click on the upper left terminal of the subtract function and select Create >> Control from the pop-up menu.

Figure 12: Create Control
A screen capture of the pop-up menu out of the subtract triangle, with the option Create, then Control selected.

Re-label x as Fahrenheit and wire the terminal as shown in Figure 13.

Figure 13: Fahrenheit Input Control
An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide.

Right click on the lower left terminal of the subtract function and select Create >> Constant and type 32.0.

Figure 14: Fahrenheit Numeric Constant
An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide. Also connected to subtract is an orange box containing the number 32.

Repeat the process to generate numeric constants for the multiply and divide function with 5.0 and 9.0 respectively.

Figure 15: Fahrenheit Numeric Constants
An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide. Also connected to subtract is an orange box containing the number 32. Also connected to multiply is an orange box containing the number 5. Also connected to divide is an orange box containing the number 9.

To complete the program, right click on the right terminal of the divide function and select Create >> Indicator. Re-label x/y as Celsius. The final diagram is shown in Figure 16.

Figure 16: Fahrenheit to Celsius G Diagram
An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide. Also connected to subtract is an orange box containing the number 32. Also connected to multiply is an orange box containing the number 5. Also connected to divide is an orange box containing the number 9. Divide is then connected at the end to an orange box labeled celsius, containing the value 1.23.

Switch to the Front Panel window to run the program. Save the program as Celsius.vi. Try various Fahrenheit values to see the corresponding Celsius values. You have successfully finished a Fahrenheit to Celsius calculator.

Figure 17
A grey grid containing two boxes. The first is labeled Fahrenheit, and contains the value 100, with up-down arrows to the left of it allowing the user to adjust the value. The second is labeled celsius, and contains the value 37.7778.

Functions

Click on empty space and drag to select the entire diagram.

Figure 18: Select G Block Diagram
An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide. Also connected to subtract is an orange box containing the number 32. Also connected to multiply is an orange box containing the number 5. Also connected to divide is an orange box containing the number 9. Divide is then connected at the end to an orange box labeled celsius, containing the value 1.23. The entire diagram has a dashed box around it.

The selected diagram is highlighted as shown in Figure 19

Figure 19: Selected G Block Diagram
An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide. Also connected to subtract is an orange box containing the number 32. Also connected to multiply is an orange box containing the number 5. Also connected to divide is an orange box containing the number 9. Divide is then connected at the end to an orange box labeled celsius, containing the value 1.23. The entire diagram is closely wrapped in a dashed border.

From the Edit menu select Create SubVI to create a G function. The resulting diagram is shown in Figure 20.

Figure 20: Creating a Function
A screen capture of a drop-down Edit window with the option, Create SubVI selected.

From the File menu select Save All and save the Untitled function as Fahrenheit to Celsius.vi.

Figure 21: Diagram with Function
An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to a function, which is connected to an orange box labeled celsius, containing the value 1.23.

Open the Fahrenheit to Celsius.vi by double clicking on the icon. Right click on the icon editor (upper right corner) and select Edit Icon…

Figure 22: Edit Icon
A drop-down menu from the function, with the option, edit icon… selected.

This pops-up the Icon Editor. Edit the function’s icon.

Figure 23: Icon Editor
The icon editor screen. There is a large white box with the large text, Fahr to Celsius. To the left is a palette with options to draw with different colors, fills, and shapes. To the right are smaller boxes replicating the large box, labeled, B and W, 16 colors, and 256 colors.

After editing the icon, the function’s icon is shown in the upper right corner of the Front Panel window. Save the function, plug in various input values and run the function. Save the function.

Figure 24: Edited Icon
A screenshot of a window titled, Fahrenheit to Celsius, with standard window options, and containing the input and output values for fahrenheit and celsius.

Close the Fahrenheit to Celsius function and return to the Celsius Block Diagram windows. The Celsius diagram reflects the updated Fahrenheit to Celsius icon

Figure 25: Function Calling
An orange box labeled fahrenheit, with the orange outside line labeled DBL, and containing the number 1.23, is connected to a box labeled Fahr to Celsius, which is connected to an orange box labeled celsius, with the orange outside line labeled DBL, and containing the number 1.23.

Case Selection

This program determines if a year is a leap year or not. A leap year is divisible by 4 but not by 100, except when it is divisible by 400. A number x is divisible by a number y if the remainder of x/y is identical to zero, i.e. Rem(x/y)=0 is true therefore

Leap Year = { Rem ( Year / 4 ) = 0 And Not ( Rem ( Year / 100 ) = 0 ) } Or Rem ( Year / 400 ) = 0 Leap Year={Rem(Year/4)=0And Not(Rem(Year/100)=0)}OrRem(Year/400)=0
(1)

where And, Or and Not are Boolean operators.

For example:

1900 is not a leap year because it is divisible by 100

1970 is not a leap year because it is not divisible by 4

1980 is a leap year because it is divisible by 4 but not by 100

2000 is a leap year because it is divisible by 400

Start a new G program and right click on the Block Diagram window. Go to the Functions >> Programming >> Numeric menu in the Block Diagram window.

Figure 26: Quotient & Remainder Function
A screen capture of a hierarchical list, beginning with Programming, and continuing with Numeric. Below the list are six shapes containing operations.

Select three copies of the Quotient & Remainder function and three numeric constants. Type in 4, 100 and 400 for the numeric constants and wire these constants to the lower input terminal (corresponding to the dividend) of the Quotient & Remainder function.

Figure 27: Leap Year Numeric Constants
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder.

From the Functions >> Programming >> Comparison menu, select 2 copies of the Equal to Zero function and one copy of the Not Equal to Zero function.

Figure 28: Comparison Functions
A screen capture of a hierarchical list, beginning with Programming, and continuing with Numeric. Below the list are four yellow triangles containing operations, named equal, not equal, equal to 0, not equal to 0.

Organize the comparison operations as show in the diagram.

Figure 29
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator.

From the Functions >> Programming >> Boolean menu select the AND and OR operators

Figure 30: Boolean Operators
A screen capture of a hierarchical list, beginning with programming, and continuing with  structures. Below the list are four objects, labeled, for loop, while loop, timed structure, case structure.

Place the Boolean operators as shown in Figure 31.

Figure 31: Q&R, Comparison & Boolean Functions
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator.

From the Functions >> Programming >> Structures menu, click on the Case Structure.

Figure 32: Case Structure
A screen capture of a hierarchical list, beginning with programming, and continuing with  structures. Below the list are four objects, labeled, for loop, while loop, timed structure, case structure.

Click and drag on the Block Diagram window to create the Case Structure.

Figure 33: Creating a Case Structure
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large dashed rectangle.

The True diagram option is indicated at the top of the case structure.

Figure 34: Created Case Structure
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large rectangle, containing a drop-down menu labeled True.

Drop a string constant and type “Is a Leap Year”.

Figure 35: True Case Editing
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large rectangle, containing a drop-down menu labeled True. Inside the box is a pink rectangle containing the text, Is a Leap Year.

Click on the down arrowhead next to the True label and select the False option.

Figure 36: Selecting the False Case
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large rectangle, containing a drop-down menu labeled True. The menu has been selected, and the option to select false is highlighted. Inside the box is a pink rectangle containing the text, Is a Leap Year.

Drop another string constant and type “Is not a Leap Year”.

Figure 37: False Case Editing
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large rectangle, containing a drop-down menu labeled False. Inside the box is a pink rectangle containing the text, Is a Leap Year.

Go to the Front Panel window and place a numeric input and an output string. Re-label the numeric input to Year and the output string to Message.

Figure 38: Leap Year GUI
A screen capture of a window with a grey grid enclosing two input or output boxes, labeled year, and message.

Right click on Year and select Representation >> I32 from the numeric pop-up menu.

Figure 39: 32-Bit Integer Numeric
A screen capture of a window with a grey grid enclosing two input or output boxes, labeled year, and message. A pop-out menu from the year box is visible, with the selection, representation, then word, then an object labeled I32 highlighted.

Arrange the Year and Message terminals in the Block Diagram window as shown in the figure.

Figure 40: Unwired Leap Year Diagram
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large rectangle, containing a drop-down menu labeled False. Inside the box is a pink rectangle containing the text, Is a Leap Year. To the far left of the diagrams is an object titled year, and to the far right is an object titled message.

Wire the OR operator is to the “?” in the case structure and the string constant “Is not a Leap Year” is wired to Message.

Figure 41: Leap Year False Case
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large rectangle, containing a drop-down menu labeled False. Inside the box is a pink rectangle containing the text, Is a Leap Year. To the far left of the diagrams is an object titled year, and to the far right is an object titled message. There are blue lines connecting year to each quotient and remainder operator, green lines connecting the operators together, then converging on a connector from the or operator to the large rectangle. Finally, there is a pink line connecting the large rectangle to an icon labeled, message.

Select the True option and Wire the “Is a Leap Year” string constant to the output terminal of the Case Structure.

Figure 42: Leap Year True Case
A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large rectangle, containing a drop-down menu labeled True. Inside the box is a pink rectangle containing the text, Is a Leap Year. To the far left of the diagrams is an object titled year, and to the far right is an object titled message. There are blue lines connecting year to each quotient and remainder operator, green lines connecting the operators together, then converging on a connector from the or operator to the large rectangle. Finally, there is a pink line connecting the large rectangle to an icon labeled, message.

Save the program as Leap Year.vi, enter Year values and run the program to determine whether the value of Year is that of a leap year or not.

Figure 43: Leap Year Program
A screen capture of a grey grid containing an input and output box. The input box is labeled, year, and contains the value 2000. The output box is labeled, Message, and contains the text, is a leap year.

Arrays

Right click on the Front Panel window and select Array from the Controls >> Modern >> Arrays, Matrix & Cluster menu, and drop an array onto the Front Panel window. The array structure consists of an index or element offset (left portion of the structure) and the array elements (right portion of the structure). When the array structure is placed on the Front Panel window, the data type of the array is undefined as indicated by the grayed out portion of the array.

Figure 44: Arrays
A two-part screenshot. The left side is a grey grid containing one white box, containing the value 0, and one larger grey box, labeled Array. The right side is a window titled, controls, containing a hierarchical list, beginning with Modern, and continuing with Array, matrix, and cluster. Below the list are two objects, labeled Array and Cluster.

To define the array data type, drag and drop a data type onto the array structure. For instance, to create an input array of numbers, place Numeric Control into the array structure.

Figure 45: Creating a Numeric Array
A two-part screenshot. The left side is a grey grid containing one white box, containing the value 0, and one larger grey box, labeled Array. Inside the grey box are dashed rectangles of different heights and widths. The right side is a window titled, controls, containing a hierarchical list, beginning with Modern, and continuing with Array, matrix, and cluster. Below the list are two objects, labeled Array and Cluster.

At this point, the numeric array is an Empty or Null array because no elements of the array have been defined. This is indicated by the grayed out numeric control within the array structure.

Figure 46: Empty Numeric Array
A screen capture of a grey grid containing an input and output box. Both boxes contain values of 0, and the box is titled, Numeric Input Array.

Define elements of an input array by selecting the offset and entering its value. For instance, at offset = 4, enter the value 0.0. This defines Numeric Input Array as {0, 0, 0, 0, 0}.

Figure 47: Defining Numeric Array Elements
A screen capture of a grey grid containing an input and output box. The box is titled, Numeric Input Array. The input box contains the value, 4, and the output box contains the value, 0.

An output array is created similarly to an input array with the exception that an output data type needs to be dropped into the array structure.

Figure 48: Creating Output Numeric Arrays
A screen capture of a grey grid containing four boxes. The left two boxes are titled, Numeric Input Array. The right two boxes are titled, Numeric Output Array. All four boxes contain the value, 0.

For Loop

This program converts an array of Fahrenheit values to Celsius. Create numeric input and output arrays and label them Fahrenheit and Celsius respectively. In the Fahrenheit array enter the values 0, 20, 40, 60, 80, 100, 120, 140, 160, 180 and 200 at offsets 0 through 10 as shown in Figure 49.

Figure 49: Numeric Input and Output Arrays
A screen capture of a grey grid containing four boxes. The left two boxes are titled, Fahrenheit, and the right two boxes are titled, Celsius. The leftmost box under fahrenheit contains the value, 10, and the right box under fahrenheit contains the value, 200. Both boxes under celsius contain the value, 0.

Right click in the Block Diagram window, navigate to Programming >> Structures and click on For Loop.

Figure 50: For Loop Structure
A screen cap of a window containing a directory hierarchy 'Programming' above 'Structures'. Contained in 'Structures' are two icons. On the left of the window there is an orange icon labeled 'Fahrenheit'.

Click and drag to create the For Loop as shown in Figure 51 and Figure 52.

Figure 51: Creating For Loops
A dashed square is in the middle of this image. There is a smal dashed square containing an 'N'in the lower right corner of the square. On the left side of the square is an icon labeled 'Fahrenheit' and on the right side there is a similar icon labeled Celsius.
Figure 52: For Loop
A dashed square is in the middle of this image. There is a smal dashed square containing an 'N'in the lower right corner of the square. On the left side of the square is an icon labeled 'Fahrenheit' and on the right side there is a similar icon labeled 'Celsius'.

Right click inside the For Loop and select Select a VI… from the pop-up menu. Find the Fahrenheit to Celsius.vi and click OK. Drop the function inside the For Loop.

Figure 53: Function in Diagram
From left to right there is an orange square labeled 'Fahrenheit' and then a big box with a blue box 'N' on the upper left corner and a blue box 'i' on the lower left corner. In the middle of the big box there is a small box containing 'Fahr to Celsius'. On the right side of the big box there is another icon labeled 'celsius'.

To complete the program, wire the Fahrenheit input array to the input terminal of the Fahrenheit to Celsius function and wire the output terminal of the Fahrenheit to Celsius function to the Celsius output array.

Figure 54: Wired Function in Diagram
From left to right there is an orange square labeled 'Fahrenheit' and then a big box with a blue box 'N' on the upper left corner and a blue box 'i' on the lower left corner. In the middle of the big box there is a small box containing 'Fahr to Celsius'. On the right side of the big box there is another icon labeled 'celsius'. An orange line runs from the right edge of the left icon through the center of the big box and connects to the left side of the right icon.

This program uses the For Loop to select each element in the Fahrenheit input array, converts that value to Celsius and saves the results in the Celsius output array. Save the program as Fahrenheit to Celsius For Loop.vi and run the program.

Figure 55: Fahrenheit to Celsius Arrays
Two parallel forms. On the left there is form labeled 'Fahrenheit' with the values '7' and '140'. On the right is another form labeled 'Celsius' with the values '7' and '60'.

The Celsius output array contains: Celsius = {-17.7778, -6.6667, 4.44444, 15.5556, 26.6667, 37.7778, 48.8889, 60, 71.1111, 82.2222, 93.3333}

While Loop

The next program will generate Fahrenheit values and convert them to Celsius until a condition is met to stop the iterations in a While Loop. In the Block Diagram window, select the While Loop structure by clicking on it from the Functions >> Programming >> Structures menu.

Click and drag to create the While Loop structure.

Figure 56: While Loop Structure
Four icons contained in the directory hierarchy under 'Programming' and 'Structures'.

Figure 57: Creating a While Loop
An empty dashed square with another small square on the bottom right corner.
Figure 58: While Loop
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle.

In the Front Panel window, create two numeric output arrays. Label them Fahrenheit and Celsius.

Figure 59: Numeric Output Arrays
A form with two rows. The upper row is labeled 'fahrenheit' and the lower row is labeled 'celsius'.

Re-arrange the diagram as in Figure 60.

Figure 60: While Loop Diagram
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'.

From the Functions menu, select Multiply function and a couple of numeric constants. Type in 20.0 and 300.0 for the numeric constants. Select the Fahrenheit to Celsius.vi and drop it inside the While Loop. Re-arrange the diagram to look like Figure 61.

Figure 61: Generating Fahrenheit Values
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'.

From the Functions >> Programming >> Comparison menu select the Greater or Equal operator.

Figure 62: Greater or Equal Function
There are four icons contained within the directory hierarchy under 'Programming' and 'Comparison'.

Wire the While Loop components as shown in Figure 63.

Figure 63: Generating Fahrenheit Values & Stop Condition
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'.

Wire the output of the Multiply operation to the Fahrenheit and the output of the Fahrenheit to Celsius function to the Celsius numeric output arrays. The connections between the While Loop and the Fahrenheit and Celsius arrays are broken (see Figure 64).

Figure 64: Broken Wires
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. The lines that connect the gray box to the right side icons are broken with red x's.

To repair the broken connections, roll over the mouse pointer to the Loop Tunnel.

Figure 65: Loop Tunnel
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. The lines that connect the gray box to the right side icons are broken with red x's. There is a mouse arrow hovering over over the right side of the gray square causing the labeled 'loop tunnel' to appear over the diagrm.

Right click on the Loop Tunnel and select Enable Indexing from the pop-up menu.

Figure 66: Enable Loop Indexing
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. The lines that connect the gray box to the right side icons are broken with red x's. There is a menu overlaid on the diagram with the item 'Enable Indexing'.

This enables values to accumulate and store the results into an array.

Repeat for the Celsius array.

Figure 67: Broken Wire Repaired
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. The line that connects the gray box to the bottom right icons is broken with a red x.

Each iteration of the While Loop in this program generates an i × 20 Fahrenheit value and converts it to Celsius. The While Loop stops iterating when the generated Fahrenheit value is greater than or equal to 300. The resulting arrays are stored in the Fahrenheit and Celsius numeric output arrays.

Save the program as Fahrenheit to Celsius While Loop.vi and run it. The program generates the following results:

Figure 68: Fahrenheit to Celsius While Loop
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'.

Fahrenheit = {0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 240, 260, 280, 300}

Celsius = {-17.7778, -6.6667, 4.44444, 15.5556, 26.6667, 37.7778, 48.8889, 60, 71.1111, 82.2222, 93.3333, 104.444, 115.556, 126.667, 137.778, 148.889}

Figure 69: Fahrenheit and Celsius Arrays
A form containing two rows. The upper level is labeled 'fahrenheit' with two fields on this level labeled containing '15' and '300'. The lower level is labeled 'celsius' and the fields contain the values '15' and '148.889'.

Graphs

Using the previous G program example, we will now visualize the results by adding a graph to the Front Panel windows. Right click on the Front Panel window. Select XY Graph from the Controls >> Modern >> Graph menu.

Figure 70: XY Graph Selection
Three icons are contained in the directory hierarchy under 'Modern' and 'Graph'.

Drop the XY Graph in the Front Panel window. Double click on the x and y axis labels and rename Time to Fahrenheit and Amplitude to Celsius.

Figure 71: XY Graph in Front Panel window
An empty graph with the x axis labeled 'fahrenheit' and the y axis labeled 'celsius'. To the left of the graph there is a couple of forms. The upper for is labeled 'fahrenheit' with the values '0' and '0'. The lower form is labeled 'celsius' with the values '0'and '0'.

The Block Diagram window contains the XY Graph terminal.

Figure 72: XY Graph Terminal in Diagram
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. To the right of this there is one final icon labeled 'XY graph'.

Select Bundle from the Functions >> Programming >> Cluster, Class & Variant menu

Figure 73: Bundle Operator
Four icons contained within the directory hierarchy under 'Programming' and 'Cluster, Class, and Variant'.

Drop it on the diagram as shown in Figure 74.

Figure 74: Bundle for XY Graph
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. In between these two black boxed arrow diagram. To the right of this there is one final icon labeled 'XY graph'.

Wire the Fahrenheit and Celsius results to the input Bundle terminals and the output Bundle terminal to the XY Graph.

Save the program and run it. The resulting graph is shown in the figure below.

Figure 75: Wired XY Graph
A gray square containing a blue boxed 'i' in the lower left corner and a green boxed red circle. There are also several icons inside the box including a orange boxed '20' and a triangle box containing an 'x' and another containing a sideways 'w'. There is also an orange boxed '300' and a box containing 'fahr to celsius'. Lines connect all of these icons together. There are On the right of this box there are two icons. The upper icon is labeled 'fahrenheit' and the bottom icon is labeled 'celsius'. In between these two black boxed arrow diagram with a pink line conntected to the right where there is one final icon labeled 'XY graph'.

Figure 76: XY Graph Result
An empty graph with the x axis labeled 'fahrenheit' and the y axis labeled 'celsius'. To the left of the graph there is a couple of forms. The upper for is labeled 'fahrenheit' with the values '0' and '0'. The lower form is labeled 'celsius' with the values '0'and '-17.7778'.

Interactivity

This G program shows how G allows programmers to develop interactive programs. Create the following G program and wire it as shown in the figure below.

Figure 77: Creating Interactive Programs
A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon which is connected to another triangular box containing  a 'x'. In the bottom right corner there is a green box containing a red circle.

In the Front Panel window, from the Functions >> Modern >> Numeric select the vertical pointer slide. From the Functions >> Modern >> Graph select Waveform Chart.

Figure 78: Vertical Pointer Slide and Waveform Chart
(a) (b)
Six icons contained within the directory hierarchy under 'modern' and 'numeric'.Six icons contained within the directory hierarchy under 'modern' and 'graph'.

Re-label the vertical pointer slide as Amplitude and the waveform chart as Sine Wave. Re-arrange to GUI to look like the figure below.

Figure 79: Slide & Waveform Chart in Front Panel window
An empty waveform chart with the x axis labeled 'Time' and the y axis is labeled 'Amplitude'.

Right click on Sine Wave and select Properties from the pop-up menu.

Figure 80: Selecting Chart Properties
An empty waveform chart with the x axis labeled 'Time' and the y axis is labeled 'Amplitude'. There is a menu overlaid the graph with the item 'Properties'.

Select the Scales tab and change Maximum to 1023. Sine Wave will display 1024 samples.

Figure 81: X-Axis Maximum
A windows window with the tab 'Scales' selected. There is a red square around the field  labeled 'Maximum' with the value '1023'.

Click on the down arrow located to the right of Time (X-Axis) and select Amplitude (Y-Axis).

Figure 82: Selecting Y-Axis
A windows window with the tab 'Scales' selected. A drop down menu with the item 'Time (X-Axis)' marked with a check mark and the item 'Amplitude (Y-Axis)' highlighted in blue.

De-select Autoscale and change the Minimum and Maximum values to -10 and 10. Click OK.

Figure 83: De-Selecting Autoscale
Figure 83 (Picture 94.png)

In the Block Diagram window, re-arrange the Amplitude and Sine Wave terminals and finish the program as shown in Figure 84.

Figure 84: Interactive Sine Wave Diagram
A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a green box containing a red circle.

Scroll the mouse pointer over the Loop Control

Figure 85: Loop Condition
A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a green box containing a red circle with a mouse arrow on top it, and the label 'Loop Condition' is overlaid.

And right click on the Loop Control and from the pop-up menu select Create Control.

Figure 86: Create Loop Control
A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a green box containing a red circle overlaid with a menu. The item 'Create Control' is highlighted in blue.

A stop terminal is created…

Figure 87: Interactive G Program
A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a'stop' icon connected to a green box containing a red circle.

With the corresponding stop Boolean input control. Save the G program as Interactivity.vi.

Figure 88: Interactive Program
An empty graph witht he x axis labeled 'Time' and the y axis labeled 'Amplitude'.

Run the G program.

Figure 89: Interactive Program
Figure 89 (Picture 102.png)

While the program is running, change the Amplitude and watch the graph update to reflect the interactive changes.

Figure 90: Interactive Program
An empty graph witht he x axis labeled 'Time' and the y axis labeled 'Amplitude'. There is a sine wave with an amplitude of 6.93878.

To end the G program, simply click on the stop button.

Congratulations. You have successfully completed and executed your first interactive G program.

Figure 91: Interactive Program
An empty graph witht he x axis labeled 'Time' and the y axis labeled 'Amplitude'. There is a sine wave with an amplitude of 6.93878. The stop button on the bottom left of the graph is highlighted with a red box.

Parallel Programming

Save a copy of Interactivity.vi as Parallel Programming.vi. Select the while loop as shown in Figure 92.

Figure 92: Select Diagram for Parallel Programming
A gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a'stop' icon connected to a green box containing a red circle.

From the menu select Edit >> Copy.

Figure 93: Copy Selected Diagram
A typical edit menu in window. The item 'copy' is highlighted in blue.

Create a copy of the while loop and its contents by selecting Edit >> Paste. Organize the diagram as shown in the figure below.

Figure 94: Paste Diagram
There are two identical diagrams. The only difference between the two are the labels. The one on the left is labeled 'Sine Wave' whereas the other is labeled 'Sine Wave 2'. The diagrams consists of a gray box containing several icons. From left to righ the icons are a orange boxed '0.02' above a blue boxed 'i' both of which are connected to a triangular box containing a 'x'. This is connected to a sine wave icon and below this icon is another icon labeled 'Amplitude'. These two icons are connected to another triangular box containing  a 'x'. To the right of the triangle is a 'Sine wave' icon. In the bottom right corner there is a'stop' icon connected to a green box containing a red circle.

Go the Front Panel window and organize the input and output controls as shown in the figure below.

Figure 95: Parallel G Program
Two parallel empty graphs.

Congratulations!!! You have just completed your first parallel interactive program using G. Save the program, run it and interact with it. To end this program click on stop and stop 2.

Figure 96: Parallel Interactive G Program
Two parallel graphs. The one on the left has a sine wave with an amplitude of about 7 and the one on the right has a sine wave with an amplitude of about 2.5.

Multicore Programming

Save a copy of Parallel Programming.vi as Multicore Programming.vi. If you have a multicore computer, CONGRATULATIONS!!! You have just completed your first multicore G program.

Figure 97: Interactive Multicore G Program
Two parallel graphs. The one on the left has a sine wave with an amplitude of about 8 and the one on the right has a sine wave with an amplitude of about 5. Overlaid on top of these graphs is another graph showing CPU Usage and CPU usage history.

Polymorphism

This program shows the polymorphic properties of G. Create the G program shown below.

Notice that the Subtract and Multiply operations allow arrays to be wired in the G program.

Figure 98: Polymorphic G Diagram
A diagram of a 'polymorphic G Diagram'. From left to right there is a blue boxed '16'connected via blue line to a large gray box containin a blue boxed 'N' in the upper left corner, an orange boxed '20'in the center and a blue boxed 'i'. The last two icon are connected via line to a triangular box containing a 'x'. A line connects this triangle to a box on the right wall of the big box which is then connected to a orange circle. Above the orange circle is an line leading to an icon labeled 'Fahrenheit'. Below the circle are three orange boxed numbers, from top to bottom '32', '5', '9'. The '32' and the circle are connected via lines to a triangular box containing a '-'. The '5' and '9' are connect via lines to a triangular box containing a '÷'. Both triangles connect to another triangle containing a 'x' and a line connects that triangle to a final icon labeled 'celsius'.

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 | 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 ...

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