comp 116: introduction to scientific programming lecture 29: file i/o

28
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Upload: byron-shepherd

Post on 17-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

COMP 116: Introduction to Scientific Programming

Lecture 29: File I/O

Page 2: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Announcements

No class this Friday

Today’s office hours moved to 3pm-4pm

Page 3: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Last class

Data types◦char, logical, integers, floating point

numbers

Page 4: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Today

File I/O◦Reading, writing MATLAB data files

(.mat)

◦Reading, writing text files

Page 5: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

JargonInput

◦Loading data from a file into MATLABOutput

◦Saving data from MATLAB into a fileText Mode

◦Files can be “read” by a human or a computer

Page 6: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

The fprintf commandFormatted output

◦Unlike disp, error commands

Usage: fprintf(format, a1, a2 ...)◦a1, a2,. … are variables◦‘format’ is a string that specifies how

the output has to be formatted

Page 7: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Examplesx is an integer with value 5 and y is a

floating point real number with value 3.2>> fprintf(‘Height is %d meters and %f cms\n', x, y)Height is 5 meters and 3.20000 cms

%d says print the corresponding variable as an integer, %f says print the corresponding variable as a floating point number‘\n’ corresponds to the newline character

Page 8: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

fprintf format string%d – print as integer%f – print as floating point number%c – print as a character%s – print as a string

%2.4f would print only upto the 4 digit after decimal

More details doc fprintf

Page 9: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Exercise I: What does this code do?

% Assume that N is preassignedfor i=1:N fprintf('The square root of %d is %f\n', i, sqrt(i));end

Page 10: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

File Input Output

Page 11: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Variable Storage (.MAT files)Loading variables

◦ load <filename>◦ .MAT extensions is assumed◦ Overwrites any current variables with same name

Writing variable(s) to a file◦ save <filename>◦ .MAT extension is automatically added◦ Saves all current workspace variables

Save only selected variables:◦ save <filename> var1 var2 var3

Page 12: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Variable Storage (.MAT files)

To see what variable(s) are in a .MAT file◦who –file <filename>

Appending more variables◦save –append <filename>

Page 13: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Reading other file formats

You've been given a file with data in it that you need.

…but it's not in the format that MATLAB saves in◦You can't get at the data by typing load <filename>

◦You're going to have to read the file yourself

Page 14: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Reading other file formats

Three steps

1. Open the file, get a file id2. Read from (or write to) the file, using

the id3. Close the file, using the id

That’s not so hard◦ Just get the syntax right

Page 15: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Opening a filefileID = fopen(filename, permission);

filename: the name of the file to open (as a string)

permission: a string saying whether we read or write, and whether the file is text or binary

fileID: a number that we will use later to read from the file

File Permissions◦ Controls whether reading, writing, or appending is

allowed.◦ How to handle a file that doesn't exist.

Page 16: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Opening a file: example

fid = fopen(‘somefile.dat’, ‘r’);

Some important (and common) file permissions◦‘r’ – read◦‘w’ – write◦‘a’ – append◦‘r+’ – read and write

Page 17: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

fopen’s return valueReturns

◦file “handle” if open was successful◦-1, if an error (e.g., trying to read a file

that does not exist)

[fid, msg] = fopen('file.dat', 'r');if (fid == -1) % Error opening file disp(msg);end

Page 18: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Just as important as opening a file◦ Possibly the thing that’s forgotten the most

Every file that we open needs to be closed when we are done using it

fclose( fileId )

Forgetting to close a file is not an error, but…◦ If you are writing to a file, the data might not get

actually saved until you call fclose.◦ There is a physical limit to how many files can be

open at once.

Closing a file

Page 19: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Writing to a fileEasier than reading, so we’ll start

with this

Writing in text mode:◦The return of the ‘fprintf’ command◦fprintf(fid, 'This is the %dth dimension\n', 8);

◦(i.e., just stick the file-id at the beginning)

Page 20: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Writing to a (text) file: example

fid = fopen('output.txt', 'wt');

fprintf( fid, 'This is the first line of output\n' );

fprintf( fid, '%d + %d = %d\n', 1, 2, (1 + 2) );

fclose( fid );

Page 21: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Exercise 2: What does this code do?

% Assume that N is preassignedfid=fopen(‘newfile.txt’, 'w');for i=1:N fprintf(fid,'The square root of %2d is %2.4f\n', i, sqrt(i));endfclose(fid);

Page 22: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Reading from a fileText file:

◦Various options

Read the file line by line, as strings◦fgetl()

oneLine = fgetl( fileId );◦Process the line with MATLAB string

functions◦Returns -1 at end of file

Page 23: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Example: Copying a text file% open input and output filesfin = fopen( 'my_orig.m', 'rt' );fout = fopen( 'my_copy.m', 'wt' ); % read and write each line of texttextLine = fgetl( fin );while ischar( textLine ) fprintf( fout, '%s\n', textLine ); textLine = fgetl( fin );end % close input and output filesfclose( fout );fclose( fin );

This is a very common ‘idiom’ for reading text files

Page 24: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Reading from a text fileRead formatted text[A,count] = fscanf(fid, format, size)◦size optional - can specify the size

of a matrix, for instance◦reads one line at a time◦returns data one column at a time

dataCol = fscanf( fid, '%f', 2 );

Page 25: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Aside: Reading/Writing Excel Fileshelp xlsread

help xlswrite

Page 26: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Common Pitfallsfopen fails

◦ Is the filename misspelled?◦ Is the directory misspelled?◦ Is the file actually in the directory you specified?

Use ‘load’ and ‘save’ when you can, use low level file I/O only when necessary

Use 'a' when trying to append more data to a file ◦ If you just use ‘w’, you’ll overwrite all the data

already in the existing file.

Page 27: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

Programming GuidelinesAlways close files that were opened

Always double check that the files were opened successfully before trying to work with them

Make sure all data is read from the file◦ Implies that you should use a “while” loop, since

you typically don’t know how many elements are in the file.

Make sure to use the correct formatting string when using fscanf or textscan◦ Test on small example string or file first

Page 28: COMP 116: Introduction to Scientific Programming Lecture 29: File I/O

ReminderReview File I/O

Practice working with text files

Read From MATLAB Help window, Contents tab,

choose MATLAB→Programming Fundamentals→Data Import and Export→Using Low-Level File I/O Functions.