CS 100 M Lecture 19 - 19. Working with Data Files Reading...

Info iconThis preview shows pages 1–18. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 19. Working with Data Files Reading from a File Writing to a File fopen, fclose, fgetl, feof, fprintf, sort Storing a Cell Array of Strings in a Data File Have: {abcd,123,xyz,3.14159} Want: abcd 123 xyz 3.14159 3 steps 1. Create and Open the File fid = fopen(MyFile.dat,'w'); The file name in quotes. w means that you can write to this file Each open file has an id. 2. Write Each String to a Line in the File C = {Line1,Line2,Line3} for i=1:length(C) fprintf(fid,'%s\n',C{i}); end File id. Print to file String Format Carriage Return 3. Close the File fclose(fid) File id. A Function to do This function Cell2File(C,fname) % C is a cell array of strings % Creates .dat file with name % specified by the string fname. % The i-th line in the file is C{i} fid = fopen([fname .dat],'w'); for i=1:length(C) fprintf(fid,'%s\n',C{i}); end fclose(fid); Example C = {abcd,123,xyz,3.14159} cell2File(C,MyFile) abcd 123 xyz 3.14159 MyFile.dat Reverse Problem Read the data in a file line-by-line and store the results in a cell array. How are lines separated? How do we know when there are no more lines? In a File there Are Hidden Markers abcd 123 xyz 3.14159 Carriage return marks end of a line eof marks end of a file Reading A File 1. Open the file. 2. Read it line-by-line until eof 3. Close the file. 1. Open the File fid = fopen(MyFile.dat,r'); The file name with suffix in quotes. r means that you can read from this file Each open file has an id. i=0; while ~feof(fid) i=i+1; C{i} = fgetl(fid); end 2. Read Each Line in the File File id names the file. Get the next line False until the end-of-file reached 3. Close the File fclose(fid) File id. A Function to Do This function C = File2Cell(fname) % fname is a string that names a .dat file % in the current directory. % C is a cell array with C{i} being the % i-th line in the file. fid = fopen([fname '.dat'],'r'); i=0; while ~feof(fid) i=i+1; C{i} = fgetl(fid); end fclose(fid); Example C = File2Cell(MyFile) abcd 123 xyz 3.14159 MyFile.dat abcd 123 xyz3.14159 C: A Detailed Read-File Example From the protein database at http://www.rcsb.org we download the file 1bl8.dat which encodes the amino acid information for the protein with the same name. We want the xyz coordinates of the proteins ``backbone. The File has a Long Header <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- saved from url=(0038)http://www.rcsb.org/pdb/files/1bl8.pdb --> <HTML><HEAD> <META http-equiv=Content-Type content="text/html; charset=windows-1252">...
View Full Document

This note was uploaded on 02/23/2008 for the course CS 100 taught by Professor Fan/vanloan during the Spring '07 term at Cornell University (Engineering School).

Page1 / 50

CS 100 M Lecture 19 - 19. Working with Data Files Reading...

This preview shows document pages 1 - 18. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online