Low Level File I/O (Updated 4/29/98)
This URL is http://www.stewart.cs.sdsu.edu/cs205/module12/
Our text discusses high-level file I/O on p. 27 with the
save data_1 x y
to save the vectors x and y computed in a script to
the data file data_1.mat.
save data_2.dat z /ascii
to save the vector z to a file data_2.dat as a
text file, i.e. ascii file that can be read by
humans.
load data_1;
restores the data that was saved previous by a script.
A useful example is presented in the MATLAB code below. Given
the file "exp.txt" exists and contains data that can be read with
the %g format, these commands will read the data, 2 numbers
per row, until the end of the file is reached.
fid = fopen('exp.txt')
a = fscanf(fid,'%g %g',[ 2 inf]) % fill the 2 by inf matrix
% In the example above, [2 inf] = 2 by inf means 2 elements per row,
% read until end of file
a = a'; % transpose
fclose(fid) % close the file
You can make this less formal by directly using the name of
the file, without the fopen command
a = fscan('exp.txt', '%g %g', [2 inf])
There is a MATLAB script file available for you to run (and examine)
today. At the MATLAB prompt:
explore_eps
explore_eps2
The new script (explore_eps2.m) is a slightly modified version of the
example (explore_eps.m) we used earlier in the semester when we
investigated Unit Roundoff
in explore_eps, unitround
examples. When you now run explore_eps2, you will notice
there is no output, where did it go? How would you find out?
type explore_eps2
or look at the source code
explore_eps2.m and its accompanying
output file explore_eps2.out.
To balance this treatment of Low Level I/O, we have been looking
at formatted input and output, i.e. numbers that people
can read. What about machine readable (binary) data?
MATLAB provides the fread and fwrite commands to manipulate
binary data.
When would this be an appropriate way to handle data?
An external file is actually a sequence of records,
often separated by an EOL, End of Line symbol for a text file.
When you begin working with external files, you also have the
opportunity (responsibility) to manipulate them. Each time you
reference an external file, you do so through the file pointer,
an operating system provided mechanism which initially points to the
very first record of the file. MATLAB provides a uniform set of
commands to rewind a file, test for the end of a file, feof,
find out exactly where you are positioned in the file, ftell,
and many other low-level operations that might be handy.
MATLAB documentation pages for I/O Details
- fprintf
-
Formatted file I/O - Write data to a file
- sprintf
- Write formatted data to a string. For example
- sprintf('%0.5g',(1+sqrt(5))/2)
- sprintf('%0.5g',1/eps)
- sprintf('%15.5f,1/eps)
- sprintf('%s','hello')
- sprintf('The array is %dx%d',2,3)
- fscanf
- Formatted file I/O - Read data from a file
- fget1
- Read formatted data from a file, discard newline character
- fgets
- Read formatted data from a file, keep newline character
- sscanf
- Read a string under format control. For example
s = '2.7183 3.1416';
A = sscanf(s,'%f') % creates the two-element vector containing poor approximations of e and pi
-
- fclose
-
- feof
-
- ferror
-
- fopen
-
- format
-
- fread
-
- frewind
-
- fseek
-
- ftell
-
- fwrite
-