HMath-3020: Working with HyperMath – Input/Output Library Continued |
![]() |
![]() |
![]() |
![]() |
|
HMath-3020: Working with HyperMath – Input/Output Library Continued |
![]() |
![]() |
![]() |
![]() |
In this tutorial, a file will be opened and then the contents of a matrix will be written to the file. Additional text will also be written to the file. Finally, the file will 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 and press ENTER. |
filenew=Open("C:/export_matrix.txt", "w")
This opens the file export_matrix.txt. This file doesn’t have to exist prior to this command being run. If the file doesn’t exist, then it will be created.
Note: Edit the file location folder if you do not have root level write permission to write in C:/
The following script can be used to check the Open operation succeeded..
filepath = "C:/export_matrix.txt"
filenew=Open(filepath,"w")
if filenew == nil then
error("fail to open file "+ "\"" + filepath + "\"");
end
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 filenew which contains the file handle that will be used for later operations. The result of this command is something similar to:
file (0574F1278)
The value within the parenthesis will be different each time the Open command is used.
Step 3: Write text to the file using both the print and write commands.
1. | In the Command window, enter the following line and press ENTER. |
print(filenew::write("This is the additional text", "\n"))
This writes "This is the additional text" in the first line of the file. The "\n" syntax was used after the string to indicate that a return is needed in the file before the next line is written. Notice how both the print and the write commands were used together.
Step 4: Create the matrix variable to be written to the file.
1. | Type the following in the Command window and press ENTER to define the matrix mat: |
mat=[1,3,5;2,4,6]
This defines a 2x3 matrix and assigns it to the variable mat. Next, the variable mat will be written to the file test_export.txt.
Step 5: Write the matrix to the file using the Fwrite command.
1. | In the Command window, enter the following line and press ENTER. |
FWrite(filenew, mat, ",")
Notice how both the F and W are capitalized in the command. Also, after the variable mat is written, a comma is listed in quotes. This indicates that the elements in the matrix should be separated by commas. Another option that could have been used is \t, which would separate the elements by a tab. If nothing is entered for this optional parameter, then a space is used to separate the elements.
Step 6: Close the file using the close command and open the file in a text editor.
1. | In the Command window, enter the following line and press ENTER. |
Close(filenew)
This closes the file.
2. | Open the file C:\export_matrix.txt in a text editor. Line one is the text that was written using the print and write commands. The matrix elements are separated by commas, as indicated in the FWrite command. |
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-3010: Working with HyperMath – Input/Output Library
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