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.
2. Create a new file using WRITEBIN.
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.
4. Evaluate M1.
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.
Using the READEXCEL and the WRITEEXCEL Functions
1. Define a two-column matrix.
2. Use WRITEEXCEL to create a new Excel file and write out the matrix to it.
3. Use READEXCEL to read in the data from the Excel file and evaluate N.
4. Transpose N.
5. Write out the transposed matrix back to the Excel file.
6. Read in the Excel file and evaluate M.
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.
2. Read in the data from the file using READFILE with no optional parameters.
The data is read in as is.
3. Use READFILE and define all the optional parameters.
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.
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.
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.
WRITEPRN writes out a submatrix of M5 as a prn file.
2. Read in the data using READPRN.
3. Use APPENDPRN to add M5 at the end of the prn file you created.
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.
The call fails. You can only append a matrix with an identical number of columns.