There are 4 important functions used to open files
in MATLAB. The descriptions here are derived from the online help. We will
only examine the simplest text file formats. More detailed help is available
online or in your textbook.
FID = fopen('filename',permission) opens the specified file with the specified permission. Permission is one of the strings:
FID = fopen('filename') assumes a permission of 'r'.
If the open is successful, FID gets a scalar MATLAB integer, the file identifier, to be used as the first argument to other FileIO routines. If the open was not successful, -1 is returned for FID.
If the file is opened in 'r' mode and it is not found in the current working directory, fopen searches down MATLAB's search path.
[A,COUNT] = fscanf(FID,FORMAT,SIZE) reads data from the file specified by file identifier FID, converts it according to the specified FORMAT string, and returns it in matrix A. COUNT is an optional output argument that returns the number of elements successfully read.
FID is an integer file identifier obtained from fopen.
SIZE is optional; it puts a limit on the number of elements that can be read from the file; if not specified, the entire file is considered; if specified, valid entires are:
FORMAT is a string containing C language conversion specifications. Conversion specifications involve the character %, optional assignment-suppressing asterisk and width field, and conversion characters d, i, o, u, x, e, f, g, s, c, and [. . .] (scanset).
If a conversion character s is used an element read may cause several MATLAB matrix elements to be used, each holding one character.
Mixing character and numeric conversion specifications will cause the resulting matrix to be numeric and any characters read to show up as their ASCII values one character per MATLAB matrix element.
fscanf differs from its C language namesake in an important respect - it is "vectorized" in order to return a matrix argument. The format string is recycled through the file until an end-of-file is reached or the amount of data specified by SIZE is read in.
Examples:
S = fscanf(fid,'%s') reads (and returns) a character string.
A = fscanf(fid,'%5d') reads 5-digit decimal integers.
COUNT = fprintf(FID,FORMAT,A,...) formats the data in matrix A (and in any additional matrix arguments), under control of the specified FORMAT string, and writes it to the file associated with file identifier FID. COUNT is an optional output argument that returns the number of bytes successfully written.
FID is an integer file identifier obtained from fopen. It can also be 1 for standard output (the screen) or 2 for standard error.
FORMAT is a string containing C language conversion specifications. Conversion specifications involve the character %, optional flags, optional width and precision fields, optional subtype specifier, and conversion characters d, i, o, u, x, X, f, e, E, g, G, c, and s. See a C manual for complete details.
fprintf behaves like ANSI C with certain exceptions and extensions. These can be found in the online help.
fprintf also differs from its C language namesake in an important respect - it is "vectorized" for the case when A is nonscalar. The format string is recycled through the elements of A (columnwise) until all the elements are used up. It is then recycled in a similar manner, without reinitializing, through any additional matrix arguments.
fclose(FID) closes the file with file identifier
FID, which is an integer obtained from an earlier fopen(). fclose('all')
closes all open files, except 0, 1 and 2. fclose() returns 0 if successful,
-1 if not.