Advanced File I/O

Chapter 9
Above: An early computer input/output device on the IBM 7030 (STRETCH)
One of the moon’s of Jupiter
(A Galilean satellite)
File I/O
Shorthand for file input and
output. Refers to the process of
reading data from files and writing
data to files
We have already covered basic file
We need to cover more advanced
low-level file I/O
Hard Disk
Memory (RAM)
Where files are stored
Where variables are stored
Permanent: Stays even after
computer is shut off
Temporary: Goes away
when computer is shut off
Slow to read/manipulate
Fast to read/manipulate
‘save’: saves a matrix as an ASCII or .mat file
• All values printed with the same formatting and delimiter
• File will have consistent number of columns
‘dlmwrite’: saves a matrix as an ASCII file
• Can specify the delimiter (must be a single character)
• File will have consistent number of columns (can be formatted)
‘load’: loads in an ASCII file or .mat file
• File must have consistent number of columns
• All data must be numeric; no formatting options
(loaded as class “double”)
What if you want to write/read a file that has…
• Different formatting in each column
• Different numbers of columns in some rows
• Has both numbers and words/characters in a single row
Variable in RAM
File on Disk
1. Writing to a file
File on Disk
2. Reading from a file
Variable in RAM
• The ‘save’, ‘dlmwrite’, and ‘load’ commands do a lot of
things automatically that we never are aware of
• Lower-level file I/O requires a more step by step process
• General steps for lower-level file I/O
1) Open the file
ead, rite, or
3) Close the file
ppend to the file
Think about opening
files in a filing cabinet
fopen: opens a file
‘r’ – open for reading (default)
‘w’ – open for writing
• Will erase/overwrite the file if
it already exists
‘a’ – open and append to file
• Will add to the end of an
existing file
• fopen returns a file identifier
• Class: double (the value is an
• Used to identify the file later
on in your code
• -1 signifies an error in opening
the file
• Warning! Opening with write permission (‘w’) will erase
the file if it exists!
• After you are finished with a file, you should close it
• Upon closing
MATLAB, all open
files are closed
• Closing files in your
code, prevents
errors and
unwanted behavior
• Returns 0 if close
was successful
• Returns –1 if there
was an error in
• Can test in an if
• Typically, you will open and close files in a script or
• Should test to make sure there are no file opening errors
• fprintf can also be used to write
to files!
• Typically used inside a loop
• ‘load’ can’t read files with numbers and characters/strings
For all of these examples, the file must be first opened with
‘fopen’ and closed with ‘fclose’ after you are finished
• ‘fscanf’ can read files with mixed content
• Data is stored in a matrix
Automatically converts characters to ASCII values
• Format of file is given in fprintf style
• Automatically converts characters to ASCII values
• ‘textscan’ can read files with mixed content
• Data is stored in a cell array
• Each cell can have different types of variables (strings, doubles, cells,
• ‘fgetl’ reads a file line by line
• Each line is stored as a string
• Use string processing with strtok to split up each line into variables
• ‘textscan’ can read files with mixed content
• Data is stored in a cell array
• Each cell can contain any type of variable (char, double, cell)
• File must already be opened with ‘fopen’
• Must close file with ‘fclose’ when finished
• ‘textscan’ stores data in a cell array
Let’s experiment with
this script and the
resulting cell array
textscan reads all lines
• Includes the header line
• Will do this even if the header starts with ‘%’
• For more details, refer to in-class demo…
in ASCII files in many formats
• If ‘load’ works, use it!
• If file has a mix of numbers and letters use ‘textscan’
• textscan is quite flexible. Use fprintf style control characters
• some files are too inconsistent to be read accurately by
• Alternative? Read in a file line by line using fgetl and
• I won’t test you on fgetl and strtok
ASCII files in many formats
• Use fprintf inside a loop and print files line by line

similar documents