Input and Output
The simplest format for data is text files. These files are frequently also called ASCII files (ASCII -- pronounced aski -- stands for American Standard Code for Information Interchange, a standard for numerically representing alphabets developed in the 1960s). There is more than one way to save the arrays we created above onto a text file.
The simplest (and least flexible) is to use an option of the command save. Assuming we want to save the data array m:
u=1:10; v=rand(1,length(u)); m = [u;v]'
m = 1.0000 0.1622 2.0000 0.7943 3.0000 0.3112 4.0000 0.5285 5.0000 0.1656 6.0000 0.6020 7.0000 0.2630 8.0000 0.6541 9.0000 0.6892 10.0000 0.7482
The command save tells MATLAB to save the contents of the variable m in text format in the file testdata.txt, in 8-digit ASCII format:
save testdata.txt -ascii m
The second option is to use the C-like mechanism of fprintf. This allows for arbitrary output formats, so it is infinitely flexible. First, we must open the file testdata2.txt for writing (the old contents are erased):
fout = fopen('testdata2.txt','w');
the permission argument 'w' will discard any existing contents in the file testdata2.txt. To append data to the end of the file, use 'a'.
We then use a vectorized version of the C fprintf statement: the format string is applied to all values in the matrix m starting by column order (i.e., the row index is the fast-changing index. This is the MATLAB convention.) That is why m has to be transposed first.
fprintf(fout,'%f %f \n',m');
The conversion character %f stands for floating-point numbers. Other choices are %d (integers), %c (sequence of characters), etc.
Note that you can try to output the numbers to the screen by using a fprintf instruction with fout set to 1:
fprintf(1,'%f %f \n',m');
1.000000 0.162182 2.000000 0.794285 3.000000 0.311215 4.000000 0.528533 5.000000 0.165649 6.000000 0.601982 7.000000 0.262971 8.000000 0.654079 9.000000 0.689215 10.000000 0.748152
After we finished writing, we use fclose to close the file.
The MATLAB command type shows us what is inside a text file:
1.0000000e+00 1.6218231e-01 2.0000000e+00 7.9428454e-01 3.0000000e+00 3.1121504e-01 4.0000000e+00 5.2853314e-01 5.0000000e+00 1.6564873e-01 6.0000000e+00 6.0198194e-01 7.0000000e+00 2.6297128e-01 8.0000000e+00 6.5407910e-01 9.0000000e+00 6.8921450e-01 1.0000000e+01 7.4815159e-01
This is equivalent to the UNIX command cat.
The simplest way of reading a file in column format, such as the one we have just written, is to use the load command. This command will automatically recognize the file as a text file, read the columns and rows, and return a matrix.
n = load('testdata.txt')
n = 1.0000 0.1622 2.0000 0.7943 3.0000 0.3112 4.0000 0.5285 5.0000 0.1656 6.0000 0.6020 7.0000 0.2630 8.0000 0.6541 9.0000 0.6892 10.0000 0.7482
Note that you can insert comment lines in the file (i.e., lines that begin with the MATLAB comment character, the percent % sign) and they will be ignored by load.
To parse more complex file formats MATLAB provides the commands fscanf. Similar to fprintf, we must open the file first:
fin = fopen('testdata2.txt','r');
the permission argument is now set to 'r', means the file is opened for reading.
The fscanf reads data from the file, converts it according to the specified format string, and returns it in matrix n2:
n2 = fscanf(fin, '%f %f', [2 inf]);
Again, %f means reading floating-point numbers only. The last argument determines the size of n2 and how much data is read. If the size is defined as [a,b], fscanf will fill a matrix of at most a rows in column order. b can be inf, but a cannot. This is why we need to make n2 to have two rows first, then transpose it:
ans = 1.0000 0.1622 2.0000 0.7943 3.0000 0.3112 4.0000 0.5285 5.0000 0.1656 6.0000 0.6020 7.0000 0.2630 8.0000 0.6541 9.0000 0.6892 10.0000 0.7482