previous: solve matrix
next: fit polynomial

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",{Number,Number}];
x = xy[[All,1]]
y = xy[[All,2]]

m = ReadList[ "m.dat", Table[Number,{5}], 3]

Matlab:

Use the "Import Wizard" (Import data from the File menu)   or

v = load('v.dat');

xy = load('xy.dat');
x = xy(:,1);
y = xy(:,2);

fid = fopen('m.dat');
m = fscanf(fid, '%f', [5,3])'
        (transpose of 5×3 !)

Maple:

xxx

xxx

xxx

IDL:

v = fltarr(25)
OpenR, 17, 'v.dat'
ReadF, 17, v
Close, 17

xy = fltarr(2,30)
OpenR, 17, 'xy.dat'
ReadF, 17, xy
Close, 17
x=reform(xy[0,*]) & y=reform(xy[1,*])

m = fltarr(5,3)
OpenR, 17, 'm.dat'
ReadF, 17, m
Close, 17