Read Data Files
12: Input numerical data from files
Read numerical data from formatted text files.(Binary files are not as straightforward; they may only be readable on the kind of computer which created the file.)
The sample data files are
| v.dat | 25 numbers, in Fortran/C "E" notation |
|---|---|
| xy.dat | 30 lines, each with an x and a y value |
| m.dat | 45 numbers, several per line |
| 1. Read in & define array v with all (25) values from the file v.dat 2. Read in & define arrays x and y with the 1st and 2nd columns (respectively) of file xy.dat 3. Read in & define 3×5 matrix M with the first 15 numbers in file m.dat |
| Mathematica:
v = ReadList[ "v.dat", Number ] xy = ReadList["xy.dat",]; m = ReadList[ "m.dat", Table[Number,5], 3] |
Matlab:
Use the "Import Wizard" (Import data from the File menu) or v = load('v.dat'); (transpose of 5×3 !) xy = load('xy.dat'); fid = fopen('m.dat'); |
| Maple:
xxx xxx xxx |
IDL:
v = fltarr(25) xy = fltarr(2,30) m = fltarr(5,3)
|
