advanced file i/o

18
Advanced File I/O Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH) http://computer-history.info/Page4.dir/pages/

Upload: olina

Post on 24-Feb-2016

55 views

Category:

Documents


0 download

DESCRIPTION

Advanced File I/O. Chapter 9. Above: An early computer input/output device on the IBM 7030 (STRETCH) http ://computer-history.info/Page4.dir/pages/IBM.7030.Stretch.dir/. Io? I/O?. Io One of the moon’s of Jupiter (A Galilean satellite) File I/O - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advanced File I/O

Advanced File I/O Chapter 9

Above: An early computer input/output device on the IBM 7030 (STRETCH)http://computer-history.info/Page4.dir/pages/IBM.7030.Stretch.dir/

Page 2: Advanced File I/O

Io? I/O?IoOne of the moon’s of Jupiter(A Galilean satellite)

File I/OShorthand 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 I/O!We need to cover more advanced low-level file I/O

Page 3: Advanced File I/O

File I/OHard Disk Memory (RAM)

File Input

File Output

Where variables are stored

Temporary: Goes away when computer is shut off

Fast to read/manipulate

Where files are stored

Permanent: Stays even after computer is shut off

Slow to read/manipulate

Page 4: Advanced File I/O

File Output: A ReviewFile Output: ‘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

Page 5: Advanced File I/O

File Output: A ReviewFile Output: ‘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)

Page 6: Advanced File I/O

File Input: A ReviewFile Input: ‘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”)

Page 7: Advanced File I/O

What’s Next: Lower-Level File I/OWhat 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

1. Writing to a file

2. Reading from a file

File on DiskVariable in RAM

File Output

Variable in RAMFile on Disk

File Input

Page 8: Advanced File I/O

Lower-Level File I/O• 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/O1) Open the file2) Read, Write, or Append to the file3) Close the file Think about opening

files in a filing cabinet

Page 9: Advanced File I/O

Opening and Closing Filesfopen: 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

integer)• Used to identify the file later

on in your code• -1 signifies an error in opening

the file

Page 10: Advanced File I/O

Opening and Closing Files• Warning! Opening with write permission (‘w’) will erase

the file if it exists!

Page 11: Advanced File I/O

Opening and Closing Files• 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

fclose• Returns 0 if close

was successful• Returns –1 if there

was an error in closing• Can test in an if

statement

Page 12: Advanced File I/O

Opening and Closing Files• Typically, you will open and close files in a script or

function• Should test to make sure there are no file opening errors

Page 13: Advanced File I/O

Writing to Files: fprintf• fprintf can also be used to write

to files!• Typically used inside a loop

Page 14: Advanced File I/O

Reading Files with Mixed Content• ‘load’ can’t read files with numbers and characters/stringsFor 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, etc…)

• ‘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

Page 15: Advanced File I/O

Reading Files with Mixed Content• ‘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

Page 16: Advanced File I/O

Reading Files with Mixed Content• ‘textscan’ stores data in a cell array

Let’s experiment with this script and the resulting cell array

FUN!!

Page 17: Advanced File I/O

Reading Inconsistently Formatted Files

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…

Page 18: Advanced File I/O

Final ThoughtsMATLAB can read 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

textscan

• Alternative? Read in a file line by line using fgetl and strtok

• I won’t test you on fgetl and strtok

MATLAB can write ASCII files in many formats• Use fprintf inside a loop and print files line by line