Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COMP 116: Introduction to Scientific Programming Lecture 29: File I/O."— Presentation transcript:

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

2 Announcements No class this Friday Today’s office hours moved to 3pm-4pm

3 Last class Data types ◦ char, logical, integers, floating point numbers

4 Today File I/O ◦ Reading, writing MATLAB data files (.mat) ◦ Reading, writing text files

5 Jargon Input ◦ Loading data from a file into MATLAB Output ◦ Saving data from MATLAB into a file Text Mode ◦ Files can be “read” by a human or a computer

6 The fprintf command Formatted 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

7 Examples x 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

8 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

9 Exercise I: What does this code do? % Assume that N is preassigned for i=1:N fprintf('The square root of %d is %f\n', i, sqrt(i)); end

10 File Input Output

11 Variable Storage (.MAT files) Loading variables ◦load ◦.MAT extensions is assumed ◦ Overwrites any current variables with same name Writing variable(s) to a file ◦save ◦.MAT extension is automatically added ◦ Saves all current workspace variables Save only selected variables: ◦ save var1 var2 var3

12 Variable Storage (.MAT files) To see what variable(s) are in a.MAT file ◦who –file Appending more variables ◦save –append

13 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 ◦ You're going to have to read the file yourself

14 Reading other file formats Three steps 1.Open the file, get a file id 2.Read from (or write to) the file, using the id 3.Close the file, using the id That’s not so hard ◦ Just get the syntax right

15 Opening a file fileID = 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.

16 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

17 fopen’s return value Returns ◦ 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

18 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

19 Writing to a file Easier 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)

20 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 );

21 Exercise 2: What does this code do? % Assume that N is preassigned fid=fopen(‘newfile.txt’, 'w'); for i=1:N fprintf(fid,'The square root of %2d is %2.4f\n', i, sqrt(i)); end fclose(fid);

22 Reading from a file Text 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

23 Example: Copying a text file % open input and output files fin = fopen( 'my_orig.m', 'rt' ); fout = fopen( 'my_copy.m', 'wt' ); % read and write each line of text textLine = fgetl( fin ); while ischar( textLine ) fprintf( fout, '%s\n', textLine ); textLine = fgetl( fin ); end % close input and output files fclose( fout ); fclose( fin ); This is a very common ‘idiom’ for reading text files

24 Reading from a text file Read 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 );

25 Aside: Reading/Writing Excel Files help xlsread help xlswrite

26 Common Pitfalls fopen 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.

27 Programming Guidelines Always 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

28 Reminder Review 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.


Download ppt "COMP 116: Introduction to Scientific Programming Lecture 29: File I/O."

Similar presentations


Ads by Google