Docsity

Working with Large Data Files, Built in Sort Function | CS 1112, Study notes of Computer Science

Toggle sidebar
bg1
CS1112 Lecture 20 2008/11/6
Lecture slides 1
๎š„Previous Lecture:
๎š„Structure
๎š„Structure arrays
๎š„Todayโ€™s Lecture:
๎š„Working with large data files
๎š„Built-in sort function
๎š„Announcement:
๎š„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
1. Open a file
fid = fopen(โ€˜geneData.txtโ€™, โ€˜wโ€™);
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
writing๎˜ƒ
November6, 2008 Lecture 20 17
2. Write (print) to the file
fid = fopen(โ€˜geneData.txtโ€™, 'w');
for i=1:length(Z)
fprintf(fid, '%s\n', Z{i});
end
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
function cellArray2file(CA, fname)
% CA is a cell array of strings.
% Create a .txt file with the name
% specified by the string fname.
% The i-th line in the file is CA{i}
fid= fopen([fname โ€˜.txtโ€™], 'w');
for i= 1:length(CA)
fprintf(fid, '%s\n', CA{i});
end
fclose(fid);
November6, 2008 Lecture 20 21
In a file there are hidden โ€œmarkersโ€
GATTTCGAG
GAGCCACTGGTC
ATAGATCCT
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);
pf2

Partial preview of the text

Download Working with Large Data Files, Built in Sort Function | CS 1112 and more Study notes Computer Science in PDF only on Docsity!

CS1112 Lecture 20 2008/11/

Lecture slides 1

ย„ Previous Lecture:

ย„ Structure ย„ Structure arrays

ย„ Todayโ€™s Lecture:

ย„ Working with large data files ย„ Built-in sort function

ย„ Announcement:

ย„ 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

1. Open a file

fid = fopen(โ€˜geneData.txtโ€™, โ€˜wโ€™);

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

2. Write (print) to the file

fid = fopen(โ€˜geneData.txtโ€™, 'w');

for i=1:length(Z)

fprintf(fid, '%s\n', Z{i});

end

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

function cellArray2file(CA, fname)

% CA is a cell array of strings.

% Create a .txt file with the name

% specified by the string fname.

% The i-th line in the file is CA{i}

fid= fopen([fname โ€˜.txtโ€™], 'w');

for i= 1:length(CA)

fprintf(fid, '%s\n', CA{i});

end

fclose(fid);

November6, 2008 Lecture 20 21

In a file there are hidden โ€œmarkersโ€

GATTTCGAG

GAGCCACTGGTC

ATAGATCCT

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

CS1112 Lecture 20 2008/11/

Lecture slides 2

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

Iterate Until End of File

November6, 2008 Lecture 20 40

Suppose x is a length n array. Then

s = sprint(โ€™%10.2fโ€™,x);

is eqiuvalent to

s = [];

for i=1:length(x)

s = [s sprintf(โ€˜%10.2fโ€™,x(i))];

end

sprintf returns a string

myMatrix.dat

fid = fopen(โ€˜myMatrix.datโ€™,'w');

[nr,nc]= size(M)

for i=1:nr

str = sprintf(โ€˜%10.4fโ€™,M(i,:));

fprintf(fid,'%s\n',str);

end

fclose(fid);

2D numeric array M ร† file

November6, 2008 Lecture 20 49

First, get the populations into an array

C = file2cellArray('StatePop');

n = length(C);

pop = zeros(n,1);

for i=1:n

S = C{i};

pop(i) = str2double(S(16:24));

end

November6, 2008 Lecture 20 55

Built-In function sort

Syntax: [y,idx] = sort(x)

X:

y:

idx:

y(k) = x(idx(k))

November6, 2008 Lecture 20 56

Sort from little to big

% C is cell array read from statePop.txt

% pop is vector of state pop (numbers)

[s,rank] = sort(pop);

Cnew = cell(n,1);

for i=1:length(C)

ithSmallest = rank(i);

Cnew{i} = C{ithSmallest};

end

cellArray2file(Cnew,' statePopSm2Lg')