HyperMath

HMath-3010: Working with HyperMath – Input/Output Library

HMath-3010: Working with HyperMath – Input/Output Library

Previous topic Next topic No expanding text in this topic  

HMath-3010: Working with HyperMath – Input/Output Library

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

In this tutorial, a file will be opened and the data will be read and assigned to variables.  The file will then be closed.

Step 1: Launch HyperMath.

1.From the Start menu, select All Programs > Altair HyperWorks > HyperMath.

This launches HyperMath in the HyperMath GUI.  Notice that by default, a file named Untitled1.hml exists in the Editor window.  By default, the Authoring Mode is displayed as well.

Step 2: Using the Command Window, open the text file.

1.In the Command window, enter the following line (after inserting the correct location for <install location>) and press ENTER.

fileid=Open("<install location>/tutorials/hmath/Tutorial_3010.txt", ‘r’)

This opens the file Tutorial_3010.txt in the reading mode.  Also, the variable fileid is assigned, which contains the file handle that is used for subsequent operations.

2.Let’s examine the file handle which was created.  Type the following in the Command window and press ENTER.

print(fileid)

This prints the value of the variable fileid which contains the file handle that will be used for later operations.  The result of this command is something similar to:

file (0572BB40)

The value within the parenthesis is different each time the Open command is used.

Step 3: Read and assign each line individually.

1.In the Command window, enter the following line and press ENTER.

line1=fileid::read()

This creates the variable line1 which contains the result of the read command.  Using the default mode for the read command, only one line is read at a time.  Therefore, the variable line1 only contains the text that is in line 1 of the file.

2.Use the print command to see what was assigned to the variable line1:

print(line1)

This results in the following:

This is line 1

3.Read in the data in line 2 and assign it to the variable line2:

line2=fileid::read()

4.Use the print command to view what was assigned to the variable line2:

print(line2)

This results in the following:

This is line 2

Step 4: Close the file using the close command.

1.To close the open file, use the close command.  Type the following in the Command window and press ENTER.

Close(fileid)

Now that the file has been read, this command closes the file.

Step 5: Reopen the file.

1.In the Command window, enter the following line (after inserting the correct location for <install location>and press ENTER.

filenew=Open("<install location>/tutorials/hmath/Tutorial_3010.txt", ‘r’)

2.Let’s examine the file handle which was created.  Type the following in the Command window and press ENTER.

print(filenew)

This prints the value of the variable fileid which contains the file handle that will be used for later operations.  The result of this command is something similar to:

file (057CF9E8)

Notice how this number is different than the one in Step 2.

Step 6: Read and assign each line individually.

1.In the Command window, enter the following line and press ENTER.

entire_file=filenew::read("*a")

This creates the variable entire_file which contains all the contents in the file.  This is because the option "*a" was used in the read command.  

2.Use the print command to see what was assigned to the variable entire_file:

print(entire_file)

The result of this command is the following:

This is line 1

This is line 2

Notice how the use of the option "*a" means that the entire file is read in at once.

See Also:

HMath-1000: Editing, Executing, Saving, and Plotting in HyperMath

HMath-1010: Working with HyperMath Authoring Mode

HMath-1020: Working with HyperMath Debugging Mode

HMath-2000: Working with HyperMath – Arithmetic and Relational Expressions and Control Structures

HMath-2010: Working with HyperMath – Logical and Relational Expressions and Control Structures

HMath-2020: Working with HyperMath – Functions and Matrix Operators

HMath-2030: Working with HyperMath – Plot Commands

HMath-3000: Working with HyperMath – String Library

HMath-3020: Working with HyperMath – Input/Output Library Continued

HMath-3030: Working with HyperMath – Batch Mode

HMath-4000: Using HyperMath Functions for Curve Fitting

HMath-4001: Using HyperMath for Material Characterization

HMath-4010: Solving Ordinary Differential Equations

HMath-4020: Solving Differential Algebraic Equations

HMath-4030: Optimization Algorithms in HyperMath

HMath-5000: Using HyperMath in HyperView Results Math

HMath-5001: Post Processing Results from FEA

HMath-5002: Registering a Function in HyperGraph 2D

HMath-5003: HyperMesh-HyperMath Cross Execution of a Tcl Script

HMath-5004: HyperMesh-HyperMath Cross-debugging of a Tcl Script