Documente Academic
Documente Profesional
Documente Cultură
Previous Lecture:
Cell arrays More on cell arrays File input/output (i/o) Discussion this week in Upson B7 computer lab Project 5 due Apr 11th at 11pm Prelim 2 on Apr 16th (Tues) at 7:30pm. Email Randy Hess (rbh27) now about any conflict and include information on the conflicting event (course number, instructor name and email, etc.)
Simple array
C S 1 1 1 2
Todays Lecture:
Each component stores one scalar. E.g., one char, one double, or one uint8 value All components have the same type
1.1 -7 12 8 1.1 -1 12
Announcements:
Cell array
C S
Each cell can store something bigger than one scalar, e.g., a vector, a matrix, a string (vector of chars) The cells may store items of different types
Lecture 19 2
Example: subset of clicker IDs I want to put in the 3rd cell of cell array C a single string. Which is correct?
A. B. C. D. E.
IDs
['d091314'; ... 'h134d83'; ... 'h4567s2'; ... 'fr83209']
L
{'h134d83', ... 'h4567s2'}
C{3} = a cat; C{3} = [a cat]; C(3) = {a cat}; Two answers above are correct Answers A, B, C are all correct
L= {}; k= 0; for r=1:size(IDs,1) if IDs(r,1)==h k= k+1; L{k }= IDs(r,:); end end Directly assign into a particular cellgood!
3
L= {}; for r=1:size(ID,1) if IDs(r,1)==h L= [L, IDs(r,:)]; end end Concatenate cells or cell arraysprone to problems!
Lecture 19
A 3-step process to read data from a file or write data to a file (Create and ) open a file Read data from or write data to the file Close the file
1. 2.
geneData.txt
3.
Lecture 19
Lecture 19
Lecture slides
CS1112 Lecture 19
1. Open a file
fid = fopen(geneData.txt ,'w'); for i=1:length(Z) fprintf(fid, '%s\n', Z{i}); end fclose(fid);
Lecture 19 10
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);
Lecture 19
11
Reverse problem: Read the data in a file line-byline and store the results in a cell array
GATTTCGAG GAGCCACTGGTC ATAGATCCT
geneData.txt
geneData.txt
How are lines separated? How do we know when there are no more lines?
Lecture 19 12 Lecture 19 13
Lecture slides
CS1112 Lecture 19
1. Open the file Read data from a file fid = fopen(geneData.txt, r);
1. 2. 3.
Builtinfunction toopenafile
Lecture 19 14 Lecture 19
15
2. Read each line and store it in cell array fid = fopen(geneData.txt, r); k= 0; while ~feof(fid) k= k+1; Z{k}= fgetl(fid); end
False until end-offile is reached
3. Close the file fid = fopen(geneData.txt, r); k= 0; while ~feof(fid) k= k+1; Z{k}= fgetl(fid); end fclose(fid);
16 Lecture 19 17
Lecture 19
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);
Lecture 19
18
Lecture 19
19
Lecture slides
CS1112 Lecture 19
Need to read past hundreds of lines that are not relevant to us.
Lecture 19
20
Lecture 19
21
14-15
N CA C O CB CG ND1 CD2 CE1 NE2 N CA C O HIS HIS HIS HIS HIS HIS HIS HIS HIS HIS TRP TRP TRP TRP A A A A A A A A A A A A A A 25 25 25 25 25 25 25 25 25 25 26 26 26 26
Read past all the header information When you come to the lines of interest, collect the xyz data
Lecture 19
22
Lecture 19
23
Suppose each line in the file statePop.txt is structured as follows: Cols 1-14: State name Cols 16-24: Population (millions) The states appear in alphabetical order.
Lecture 19
27
Lecture 19
30
Lecture slides
CS1112 Lecture 19
A detailed sort-a-file example Create a new file statePopSm2Lg.txt that is structured the same as statePop.txt except that the states are ordered from smallest to largest according to population.
Alabama Alaska Arizona Arkansas California Colorado : : 4557808 663661 5939292 2779154 36132147 4665177 : :
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
Need the pop as numbers for sorting. Cant just sort the pop have to maintain association with the state names.
Lecture 19 32
Converts a string representing a numeric value (digits, decimal point, spaces) to the numeric value scalar of type double. E.g., x=str2double( 3.24 ) assigns to variable x the numeric value 3.24
33
10 5 3
20 10 1
90 15
y:
15 20 90 5 2 4
idx:
10 5 3
20 10 1
90 15
10 5 3
20 10 1
90 15
y:
15 20 90 5 2 4
y:
15 20 90 5 2 4
idx:
idx:
y(k) = x(idx(k))
Lecture 19
41
Lecture slides
CS1112 Lecture 19
Wyoming Vermont North Dakota Alaska South Dakota Delaware Montana : : Illinois Florida New York Texas California
509294 623050 636677 663661 775933 843524 935670 : : 12763371 17789864 19254630 22859968 36132147
Lecture 19
44
Lecture slides