File Access


Examples

Writing a Matrix to an External File

If you have data stored in an $n \times m$ matrix and you want to output the values to an external file, you need to write out the matrix element by element.

For example, suppose you have a matrix $\mb{X}$ that contains data that you want written to the file USER.MATRIX. Suppose also that $\mb{X}$ contains ones and zeros so that the format for output can be one column. You need to do the following:

  1. Establish a fileref, such as OUT.

  2. Use a FILE statement to open the file for output.

  3. Specify a DO loop for the rows of the matrix.

  4. Specify a DO loop for the columns of the matrix.

  5. Use a PUT statement to specify how to write the element value.

  6. End the inner DO loop.

  7. Skip a line.

  8. End the outer DO loop.

  9. Close the file.

Your statements should look as follows:

   filename out 'user.matrix';
   file out;
   do i = 1 to nrow(x);
      do j = 1 to ncol(x);
         put (x[i,j]) 1.0 +2 @;
      end;
      put;
   end;
   closefile out;

The output file contains a record for each row of the matrix. For example, if your matrix is $4 \times 4$, then the file might look as follows:

   1  1  0  1
   1  0  0  1
   1  1  1  0
   0  1  0  1

Quick Printing to the PRINT File

You can use the FILE PRINT statement to route output to the standard print file. The following statements generate data that are output to the PRINT file:

   > file print;
   > do a = 0 to 6.28 by .2;
   >    x = sin(a);
   >    p = (x+1)#30;
   >    put @1 a 6.4 +p x 8.4;
   > end;

Here is the resulting output:

   0.0000                                0.0000
   0.2000                                     0.1987
   0.4000                                           0.3894
   0.6000                                                 0.5646
   0.8000                                                     0.7174
   1.0000                                                         0.8415
   1.2000                                                           0.9320
   1.4000                                                             0.9854
   1.6000                                                             0.9996
   1.8000                                                             0.9738
   2.0000                                                           0.9093
   2.2000                                                        0.8085
   2.4000                                                    0.6755
   2.6000                                               0.5155
   2.8000                                          0.3350
   3.0000                                    0.1411
   3.2000                             -0.0584
   3.4000                       -0.2555
   3.6000                 -0.4425
   3.8000            -0.6119
   4.0000        -0.7568
   4.2000    -0.8716
   4.4000  -0.9516
   4.6000 -0.9937
   4.8000 -0.9962
   5.0000  -0.9589
   5.2000    -0.8835
   5.4000       -0.7728
   5.6000            -0.6313
   5.8000                 -0.4646
   6.0000                      -0.2794
   6.2000                            -0.0831