CS 211 Lesson 23
File Input/Output
Quote:
There's nothing so rewarding as to make people realize they are worthwhile in this world. Bob Anderson
Lesson Objectives:
Understand the difference between text (formatted) files and binary (unformatted) files.
Know what MATLAB functions are available for reading and writing files.
Understand the concept of opening and closing a file.
Be able to format text output using fprintf
Understand the MATLAB functions that manipulate entire files and folders
Lesson:
I. MATLAB Concepts
A. File Input/Output (I/O)
File input/output refers to reading data from (and writing data to) a file that is saved on a computer's external memory (e.g., hard drive, memory key, network drive, tape drive, etc.).
Files provide persistent storage of data sets while they are not being processed by the CPU. Remember than any data in a computer's main memory (RAM) is lost when a computer shuts down.
Files can be classified in two broad types: text files and binary files.
MATLAB provides a variety of functions for both text and binary file input and output.
The following table compares text and binary files:
Text (formatted) I/O Binary (unformatted) I/O Advantages Text files can be opened with a variety of text editors (Notepad, WordPad, Microsoft Word, etc.) and can be viewed and/or modified easily by a user.
Data is stored as efficiently as possible. Therefore, large data sets use the least possible amount of disk space. Disadvantages The encoding of the data is not optimized. Therefore, for large data sets the file size can grow very large.
The data cannot be viewed and/or manipulated easily.
When to use Use text I/O when the size of data files is not a concern. Use binary I/O when you have very large data sets and you need to conserve memory and disk space.
Some examples MATLAB .m files
"text" .txt files
Web hypertext .htm filesMATLAB .mat files
MATLAB .p files
Excel .xls files
Music .mp3 files
Video .mpg files
MATLAB
input functionsfscanf()
fgetl()
fgets()
textread()
textscan()
fread()
load()
MATLAB
output functionsfprintf() fwrite()
save()
MATLAB file related functions fopen()
fclose()
feof()
ferror()
fopen()
fclose()
feof()
ferror()
fseek()
ftell()
B. File access
There are 3 basic steps to accessing data in a file:
Open the file
Read data from the file OR write data to the file (but typically not both)
Close the file
To open a file:
Call the File_ID = fopen(File_name, Access_mode) function.
The first argument to fopen() is a string (a row vector of characters) that contains the name of the file.
If the file name contains no folder path, then MATLAB will look for the file in its current "working folder". If the file is not found, MATLAB will search the folders defined in its "search paths" and open the first file it finds with a matching name. For example, the function call:
File_ID = fopen('example.txt','r');
will find the file "example.txt" only if the file is in the current working folder or in a folder that MATLAB searches.
If the file name contains a folder path, then MATLAB will only look in that specific folder for the file. For example,
File_ID = fopen('C:\Documents and Settings\Fred\example.txt','r');
will find the file "example.txt" only if it exists in the folder "C:\Documents and Settings\Fred".
The second argument to fopen() is a string that specifies the type of access that is desired to the file. The options are:
'r' open an existing file for reading only (default permission) 'r+' open an existing file for reading and writing 'w' delete/create a file and open it for writing 'w+' delete/create a file and open it for reading and writing 'a' open/create a file for appending (writing data after the existing contents of the file) 'a+' open/create a file for reading and appending
Opening a file for reading ('r') will never modify the file in any way.
Opening a file for writing ('w' or 'a') will change the file.
If the file already exists and contains data, writing ('w') to the file will destroy all of its previous contents.
If the file does not currently exist, writing ('w') to the file will create a new file.
- If the file already exists and contains data, appending ('a') to the file will add new data to the end of the file. All of its previous contents remain unchanged.
- If the file does not currently exist, appending ('a') to the file will create a new file.
To read (or write) data to a file
To read data from a file:
text file, use one of: fscanf(), fgetl(), fgets(), textscan() binary file, use: fread() To write data to a file:
text file, use: fprintf() binary file, use: fwrite()
To close a file:
Call the fclose(File_ID) function.
Attempts to access a file after it is closed will produce a run-time error.
There are three MATLAB commands that do not use an open / process / close sequence. These three commands require no open or close operation on a file.
load() Reads a binary file containing MATLAB variables and creates those variables in the current workspace. store() Writes variables in the current workspace to a binary file. textread() Reads a text file that is organized as a table of data values into a series of parallel arrays. C. Review of fprintf()
The fprintf() function writes formatted text to a file or the command window.
If the first argument is a "file identifier," then the output is sent to the file associated with the "file identifier."
If the first argument is a "format string", then the output is sent to the command window on the user's screen.
The fprintf() function writes (outputs) a format string exactly as it is specified, and replaces each format specifier with a value from the fprintf function's argument list. (See examples below.)
Special escape sequences, beginning with the backslash '\' character, are used to represent "non-printable" characters. The most commonly used escape sequences are:
\n a "new line" character (start a new line of output) \t a "tab" -- spaces over to the next tab stop \r a "carriage return" \\ output a single backslash A format specifier begins with the % character and is followed by other characters that precisely describe how the corresponding output value will be formatted. A format specifier always contains a single letter (format Descriptor) that describes the data type of the output value. The most commonly used data types are:
%d display the value as an integer (this only works if the number really is an integer) %e display the value in exponential format (3.23e5) %f display the value in floating point format (3.451) %g display the value showing only significant digits -- use this most of the time %s display the value as a string - a series of UNICODE characters %c display the value as a single UNICODE character A format specifier is defined exactly as follows, where the modifier, fieldWidth, and precision values are all optional:
% modifier fieldWidth . precision formatDescriptor
Modifiers Description - left-justifies output in its field (no minus sign means right-justify) + always print a plus or minus sign with the output value 0 pad the field with leading zeros fieldWidth - an integer that specifies how many spaces to use for the displayed value. If the field is too small, the fprintf() function will expand the field to make it large enough to display the entire value.
precision - only applies to floating point numbers. It specifies how many digits to display after the decimal point.
The following is a simple example using fprintf() to write formatted output to a file. Please study the format specifiers contained in the example fprintf() statements to confirm your understanding of how format specifiers work.
File_ID = fopen('demo_file.txt', 'w');
fprintf(File_ID, '12345678901234567890\n');
fprintf(File_ID, '%10d\n', floor(pi));
fprintf(File_ID, '%9d\n', floor(pi));
fprintf(File_ID, '%8d\n', floor(pi));
fprintf(File_ID, '%g\n', pi);
fprintf(File_ID, '%f\n', pi);
fprintf(File_ID, '%10.2f\n', pi);
fprintf(File_ID, '%0.2f\n', pi);
fprintf(File_ID, '%c\n', 'Test');
fprintf(File_ID, '%s%s\n', 'Test', 'text');
fprintf(File_ID, '%10s%10s\n', 'Test','text');
fprintf(File_ID, '%-10s%-10s\n', 'Test','text');
fclose(File_ID);The above commands produce the following formatted text file:
12345678901234567890 3 3 3 3.14159 3.141593 3.14 3.14 T e s t Testtext Test text Test textD. File and Folder Manipulation
MATLAB has several commands that manipulate entire files, such as:
copyfile('source', 'destination') Copies the entire contents of one file into another file. movefile('source','destination') Moves a file from one folder to a different folder. delete('FileName') Deletes a file.
MATLAB has several commands that manipulate folders, such as:
s = pwd() Return the name of the "present working directory." cd('FolderName') Makes the specified folder the "present working directory." files = dir('FolderName') Returns information about all the files in the specified folder. mkdir('FolderName') Make a new sub-folder in the "present working directory." rmdir('FolderName','s') Remove (delete) a folder and all the files in the folder.
II. Good Programming Practices
Always close a file immediately after its use. Not closing a
file that you are writing to can potentially cause the loss of some data.
Use binary files only when speed of data access or large
file sizes are an issue.
Be very careful when writing to a file. If the file already
exists, the previous contents will be replaced (destroyed) by the new data.
III. Algorithms
none for this lesson
Lab Work: Lab 23
References: Chapman Textbook: section 8.4, 8.6, 8.7