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:

Lesson:

I. MATLAB Concepts

A. File Input/Output (I/O)

  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 files
MATLAB .mat files
MATLAB .p files
Excel .xls files
Music .mp3 files
Video .mpg files
 
MATLAB
input functions
fscanf()
fgetl()
fgets()
textread()
textscan()
 
fread()
load()
 
MATLAB
output functions
fprintf() fwrite()
save()
 
MATLAB file related functions fopen()
fclose()
feof()
ferror()

 
fopen()
fclose()
feof()
ferror()
fseek()
ftell()
 

B. File access

C. Review of fprintf()

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 above commands produce the following formatted text file:

D. File and Folder Manipulation

II. Good Programming Practices

III. Algorithms

Lab Work: Lab 23

References:  Chapman Textbook: section 8.4, 8.6, 8.7