

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


ย Structure ย Structure arrays
ย Working with large data files ย Built-in sort function
ย P5 posted, due 11/13 at 6pm ย Prelim 3 on 11/18. Tell us now if you have a conflict
November6, 2008 Lecture 20 15
An open file has a file ID, here stored in variable fid
Builtโin function to open a file
Name of the file (created and) opened. txt and dat are common file name extensions for plain text files
โwโ indicates that the file has been opened for w riting
November6, 2008 Lecture 20 17
Printing is to be done to the file with ID fid
Substitution sequence specifies the string format followed by a new-line character
The ith^ item in cell array Z
November6, 2008 Lecture 20 19
November6, 2008 Lecture 20 21
geneData.txt
Carriage return marks the end of a line
eof marks the end of a file
November6, 2008 Lecture 20 26
function CA = file2cellArray(fname) % fname is a string that names a .txt file % in the current directory. % CA is a cell array with CA{k} being the % k-th line in the file.
fid= fopen([fname '.txt'], 'r'); k= 0; while ~feof(fid) k= k+1; CA{k}= fgetl(fid); end fclose(fid);
November6, 2008 Lecture 20 34
fid = fopen(โ1bl8.datโ, โrโ); x=[];y=[];z=[]; while ~feof(fid) s = fgetl(fid); if strcmp(s(1:4),'ATOMโ) if strcmp(s(14:15),'CAโ) x = [x; str2double(s(33:38))]; y = [y; str2double(s(41:46))]; z = [z; str2double(s(49:54))]; end end end fclose(fid);
November6, 2008 Lecture 20 40
November6, 2008 Lecture 20 49
November6, 2008 Lecture 20 55
y:
idx:
November6, 2008 Lecture 20 56