Department of Applied Mathematics at the University of Colorado at Boulder
University of Colorado at Boulder Search A to Z Campus Map University of Colorado at BoulderCU Search Links
Print this page

Write Data to a File

20: Write data to a file

For this example, A is a 10×3 array of values, where the first column are the x values 1 through 10, the second column are the y values where y=sqrt(x), and the third column are the z values where z=x2.
 1    1.        1
 2    1.41421   4
 3    1.73205   9
 4    2.        16
 5    2.23607   25
 6    2.44949   36
 7    2.64575   49
 8    2.82843   64
 9    3.        81
10    3.16228   100

1. create 10×3 array A
2. write the values of A to a text file A.dat, one row per line
3. generate a 7×11 matrix R of random values (uniform on [0,1]), and
  write them to file ranm.dat, four elements per line

Mathematica:

(1)
A = Table[,]

(2)
Export["A.dat", A, "Table"]

(3)
R = Table[Random[], 7,11];

R4 = Partition[Flatten[],4]
OutputForm[TableForm[R4,TableSpacing->0]]>>ranm.dat

Matlab:

(1)
xxx

(2)
xxx

(3)
xxx

Maple:

(1)
xxx

(2)
xxx

(3)
xxx

IDL:

(1)
A = fltarr(3,10)
A[0,*] = findgen(10)+1
A[1,*] = sqrt(A[0,*])
A[2,*] = A[0,*]^2

(2)
OpenW, 22, 'A.dat'
PrintF, 22, A
Close, 22

(3)
R = randomu(seed, 7, 11)
OpenW, 22, 'ranm.dat'
PrintF, 22, R, FORMAT='(4F9.6)'
Close, 22