23
File I/O 1. Goals of this chapter 2. Inputs Files 3. Output Files 4. File Formats 5. General Concept, lots of examples 6. High-Level vs. Low-Level 1

File I/O 1. Goals of this chapter 2. Inputs Files 3. Output Files 4. File Formats 5. General Concept, lots of examples 6. High-Level vs. Low-Level 1

Embed Size (px)

Citation preview

1. Goals of this chapter

Understand why external files are greatly used in programming, such as .txt, .xls, .dat…

Recognize when to use a file vs. hardcoding data Understand how Matlab sees any file

Define and recognize delimiting-characters

Depending on which type of file, choose an appropriate built-in function

Remember the overall idea behind each built-in function Remember the logical order of reading/writing a file

2

4. File Formats

Files are stored in essentially two different formats: ASCII text, and binary.

ASCII text consists entirely of characters which have meaning within the ASCII table. The only special codes are already present in the ASCII table, such as newlines and tabs. Notepad, for example, works only with ASCII text files.

10

4. File Formats, cont.

Binary files may contain some ASCII text, but will also contain data which has meaning to the program which was intended to use the file. MS Word, for example, embeds codes for margins, bold, underline, HTML, etc in the file. These codes only have meaning to Word – not to programs like Notepad.

Idea: Word can work with Notepad files, but Notepad cannot work with Word files. 11

5. General Concept

When reading data from a file, Matlab continually requests values from the file data stream until the end of the file is reached. As the data is received, the program must identify the delimiting

characters (tabs, spaces, enter key, special characters..) and reformat the data to reconstruct the organization of the data as represented in a file.

When writing data to a file: to preserve the organization of the data, the appropriate delimiting characters must be inserted into the stream of information.

12

File ProgramAn Input Stream of information

Program FileAn Ouput Stream of information

5. General Concept, cont.

Using the data contained inside input-files Analyze, min, max, average Estimate trends (stock market) Use it as a database for one or two variables (coefficient of

friction) Use it to give user possibilities to pick from Plot

13Examples…

5. General Concept, cont.

Using the data contained inside input-files Analyze, min, max, average Estimate trends (stock market) Use it as a database for one or two variables (coefficient of

friction) Use it to give user possibilities to pick from Plot

Using output-files, once populated with data Print them Email them Publish in a report Reuse as an input file for another software

18

5. General Concept, cont. Although there are only a few ADVANTAGES to using

files, the ones that do exist are sufficient in themselves!!!

1. LOTS of data, usually contained in very little variables (possibly only 1 cell-array!)

2. The software is developed knowing that the file cannot change format, but definitely knowing that the file can grow in size!

Although the hurricane data obviously gets updated yearly, the software to analyze the data does not change a bit!

3. User does not have to enter the data: less prone to errors.

4. Long term saving/backup of data.19

5. General Concept, cont.

There are two types of built-in functions High-level functions: one (and 1 only) command opens the file,

read or writes data, and closes the file. All is stored immediately in variable(s).

Low-level functions: 1. one command needs to open the file,

2. another command (maybe more than one) needs to read or write the data,

3. and finally another command closes the file.

22Each of these concepts is detailed in separate powerpoint lessons…

Wrapping Up

What is an input file? What is an output file? What examples of input files were shown? What examples of output files were shown? What can be done with output files? What can be done with input files? What is the difference between high-level and low-level

I/O functions?

23