Compose-3010: Read and Write ASCII Files in OML

This tutorial explains how to:
  • Read content from an ASCII file.
  • Write data in an ASCII file.

Read an ASCII File

In Compose OML, there are several ways to read an ASCII files. You may read it all at one time, read the file line by line, or you may read it by specifying a format for the data fields. You can choose the most appropriate way. This tutorial demonstrates how to read in these methods.

  1. Read all content of an ASCII file.

    You can use the type function to display the content of the specified file. You do not need to open and close the file, as shown in the example below:

    content = type('read1.txt');

    You may find read1.txt under <install/tutorials/.

    Run this script in the Editor or OML Command window.

    This is a simple way to check the content of the ASCII file.

  2. Read the ASCII file line by line.

    When the file is large and contains multiple lines, and the application requires you to manipulate specific lines or strings, it may be more efficient to read the file line by line:

    file = 'read1.txt';
    fid = fopen(file,'rt');
    counter = 0;
    % read file line by line
    while ~feof(fid)
       counter = counter + 1;
       line = fgetl(fid);
       sprintf('line %d in %s: %s',counter,file,line)

    This returns:

    If you want to read data from a file, you must first open it. This is done by using fopen('/Path/to/File') in the Editor window:

    fid = fopen('read1.txt','rt');

    This assigns a file ID to the variable fid that can be referenced later on. The second input of fopen is the option to be used. In this case, use rt for read, which means the file is read in text mode. fgetl(fid) function returns the whole line content in the file fid. feof(fid) is used to decide if fid has encountered the end of the file.

    Other tests may be used to stop reading the file. Please refer to tutorial Compose-3015 for more information.

  3. Read an ASCII file with formatted fields using fscanf.

    Sometimes data with the ASCII format is saved in a specific format. The function fscanf can handle this case. First, a file must be created:

    fid = fopen('read2.txt','wt');
    fprintf(fid,'%4s %8s\n','node','displacement');
    for i = 1:100
       fprintf(fid,'%4d %8s\n',i,num2str(rand*10));

    Open read2.txt, created in OML, and you’ll notice the format looks like this:

    Try to read this file using fscanf:

    fid = fopen('read2.txt','rt');
    A = fscanf(fid,'%d %f\n',[2 inf]);

    Run this script in the Editor window; the results are printed in the OML Command window:

    Note that the result saved in matrix A' is the same as the one just saved in read2.txt. To breakdown this script:

    fseek(fid,18) is used to update the file pointer so as to jump the first line in read2.txt.

    fscanf(fid,’%d %f\n’,[2 inf]) matches a matrix with two rows and as many columns as possible using the format ‘%d %f\n’.

  4. Read an ASCII file with formatted fields using the textread function.
    You can also use textread to fulfill the same job as fscanf, as shown in the example below:
    file = 'read2.txt';
    [A,B] = textread(file,'%d %f','headerlines',1);
    out = [A,B];

    This results in:

    The second input argument of textread() specifies the format, while the third and fourth input arguments lead you to skip the first line of read2.txt (one header line).

Writing to a File

Note: If Compose is installed in a restricted directory, executing the write commands is not possible. It is recommended to execute this tutorial in a user directory or any directory with write permissions.
  1. Simple example

    To write to an ASCII file using fprintf, first open the file as previously indicated, but instead open it in writing mode instead of reading mode using the following code.

    fname = 'read3.txt';
    fid = fopen(fname, 'wt');
    fprintf(fid, 'Hello World');

    This outputs 'Hello World' to the file read3.txt, the second input argument of fopen ‘wt’ means write in text file mode. Note that fwrite is used to write a binary file rather than ASCII file.

  2. Save a matrix.

    In this tutorial, you’ve learned how to save float scalars with special formatting in a ASCII file with formatted fields using fscanf. Saving a matrix into an ASCII file is shown below:

    % write matrix in read4.txt
    x = [0:0.1:2*pi];
    y = [x',sin(x)'];
    fid = fopen('read4.txt','wt');
    fprintf(fid,'%8s %8s\n','X','Y')
    for i = 1:size(y,1)
          fprintf(fid,'%f %f\n',y(i,1),y(i,2));

    This example shows how to save a matrix to an ASCII file. See the results in read4.txt after evaluating this script:

    Note that precision can be lost when saving the matrix in an ASCII file. It’s better to save it in a binary file or use the Save/Load functions in OML.