Functions > Reading and Writing Files > Example: Reading and Writing Data Files
Example: Reading and Writing Data Files
Use file access functions to read and write to binary, Excel, data, text, and PRN files.
Using the READBIN and the WRITEBIN Functions
1. Define a matrix.
Click to copy this expression
2. Create a new file using WRITEBIN.
Click to copy this expression
In this case, WRITEBIN writes out matrix M to a binary file, the data format is set to byte, and little-endian is used.
3. Use READBIN to read in the binary file, with no optional parameters.
Click to copy this expression
4. Evaluate M1.
Click to copy this expression
By default, READBIN reads in the data as a vector. READBIN reads in the lines one by one into vector M1.
5. Use READBIN to read in the binary file and set the optional parameters to read in the data as a matrix of 5 columns.
Click to copy this expression
Click to copy this expression
Using the READEXCEL and the WRITEEXCEL Functions
1. Define a two-column matrix.
Click to copy this expression
2. Use WRITEEXCEL to create a new Excel file and write out the matrix to it.
Click to copy this expression
3. Use READEXCEL to read in the data from the Excel file and evaluate N.
Click to copy this expression
Click to copy this expression
4. Transpose N.
Click to copy this expression
5. Write out the transposed matrix back to the Excel file.
Click to copy this expression
6. Read in the Excel file and evaluate M.
Click to copy this expression
Click to copy this expression
Some cells of the original matrix are still there. The original 1.xlsx file contained 2 columns and 5 rows. In step 5, WRITEEXCEL wrote over the top two rows. It did not overwrite the bottom part of the original two columns that is cells A3 through B5. READEXCEL padded the empty cells with NaNs, to create a complete matrix.
Using the READFILE and the WRITEFILE Functions
1. Define a matrix and use WRITEFILE to write it out.
Click to copy this expression
Click to copy this expression
2. Read in the data from the file using READFILE with no optional parameters.
Click to copy this expression
Click to copy this expression
The data is read in as is.
3. Use READFILE and define all the optional parameters.
Click to copy this expression
Click to copy this expression
In this case, the parameters set the READFILE function to:
read in the 1.dat file
read in the lines as fixed-column-width data
read in each column as if its width is a single character
read in rows 1 and 2
read in all the columns
pad spaces with -1
use “.” (dot) as a decimal symbol
* 
When referring to matrix indices, read functions start at 1.
Using the READTEXT and the WRITETEXT Functions
1. Define a matrix and write it out using WRITETEXT.
Click to copy this expression
Click to copy this expression
In this case, WRITETEXT writes out a submatrix of M3, starting from index (1,1) and ending at index (5,5). The ranges are defined using two 2-element vectors.
* 
When you write out a text file, the matrix indices start at (1,1) and not at (0,0). The value of cell (1,1) is 5 but in order to write it out you must specify cell (2,2).
2. Read in the data using READTEXT.
Click to copy this expression
Click to copy this expression
READTEXT reads in the top part first two columns and the fourth column. The range is defined using strings. The \t parameter specifies that the numbers are separated by a tab.
Using the WRITEPRN and the APPENDPRN Functions
1. Define a matrix and write a submatrix of it to a file using WRITEPRN.
Click to copy this expression
Click to copy this expression
WRITEPRN writes out a submatrix of M5 as a prn file.
2. Read in the data using READPRN.
Click to copy this expression
Click to copy this expression
3. Use APPENDPRN to add M5 at the end of the prn file you created.
Click to copy this expression
APPENDPRN adds the data at the end of the file, below the original matrix.
4. Use APPENDPRN to append a matrix with a different column number.
Click to copy this expression
The call fails. You can only append a matrix with an identical number of columns.
Was this helpful?