Title: Class Prep
1Class Prep
- Bring to Class
- In-Class Exercises
- Paper for Classroom Printer
- Run MATLAB
- Set Correct Current Directory
2Grab Files
- Copy the data file file_1.txt, data2.xls,
gettysburg.txt, studs.m, studs.txt, phones.txt,
table1.m, table2.m, table3.m, temps.txt from
70week06 of the csc070 course folder into your
sub-folder - Run MATLAB and set the Current Directory to your
sub-folder.
3Go Over HW 5
4REMINDER Test 2 Next Week
- See http//cs.union.edu/csc070
- Link to Schedule, then Lecture 07b
- Download Test
- I will be happy to answer questions about the
test after class today and/or before/after class
on Monday and/or Monday afternoon office hours. - Note that time may be a factor, so be sure that
you know the material well. (You won't have time
to look everything up in the textbook.) - You may bring up to 4 cheat sheets (single or
double-sided) to the test.
5Algorithm Study for CSc-70 Test
- Take Practice Test
- Don't understand something? Email or see HannayD
- Redo In-Class Exercises
- Don't understand something? Email or see HannayD
- Redo HW Without Help from Others
- Don't understand something? Email or see HannayD
- Go Over Lecture Notes
- Don't understand something? Email or see HannayD
- Study Textbook, make up "Cheat Sheets"
- Don't understand something? Email or see HannayD
6Week 06-c(8.1-8.5)
- High-Level I/O
- File Input
- File Output
7Garbage In, Garbage Out
8Review MATLAB Workspace I/O
- MATLAB provides the tools to save your complete
workspace to a file with the save command and
reload it with the load command. - If you provide a file name with the save command,
a file of that name will be written into your
current directory. - If you do not provide a file name, MATLAB saves
the workspace as matlab.mat. - You can also identify specific variables that you
want to saveeither by listing them explicitly or
by providing logical expressions to indicate the
variable names. For example - gtgt save mydata.mat a b c
- would save the variables a and b and any
variable beginning with the letter c (recall in
regular expressions...).
9General Data Import and Export
- The next lower level of file I/O describes
working with entire files at once, but files
whose contents are not explicitly MATLAB
variables. - For example, working with spreadsheets, images,
audio, and video files, or raw ASCII text. - There is no explicit open or close of the
file stream. - First we will concentrate on two file formats
- Excel Spreadsheets
- Delimited text files
10Excel Spreadsheets
- Typical spread sheets have some rows and columns
of numbers, with rows and columns of text
surrounding them - MATLAB provides the function xlsread() that
separates these items
11Working with Spreadsheet Data
- xlsread( ) - read data from an Excel spreadsheet
- (Double-Clicking Spreadsheet Loads numbers as a
Matrix) - m xlsread('filename.xls') gets data
- num, txt xlsread('filename.xls') puts numeric
data into num, text (column headings) into txt - No open/close required
gtgt num, txt xlsread('data2.xls') gtgt help
xlsread
12Hands-On DEMO Getting Data from Excel
- Run MS Excel
- Enter Header Row with 3 columns
- Time Temperature Pressure
- Enter 3 rows of numbers
- 10 99.8 349.43
- 12 103.9 450.2
- 15 127 569.8
- Save the file in your sub-folder as data.xls
- In MATLAB, get the data using xlsread()
- values, heading xlsread('data.xls')
13MATLAB Implementation
- gtgt nums txt raw xlsread('grades.xls')
- nums
- 19 78
- 22 83
- 98 99
- 21 56
- 23 89
- 19 51
txt 'name' 'age' 'grade' 'fred' '' '' 'joe' ''
'' 'sally' '' '' 'charlie' '' '' 'mary' ''
'' 'ann' '' ''
raw 'name' 'age' 'grade' 'fred' 19
78 'joe' 22 83 'sally' 98
99 'charlie' 21 56 'mary' 23
89 'ann' 19 51 'ann' 19 51
14Excel Spread Sheet Summary
- nums txt raw xlsread('grades.xls')
- nums contains the rectangle enclosing all the
numerical values - Non-numeric cells within that rectangle appear as
NaN - txt contains the alphanumeric rows and columns as
a cell array of strings - The numeric values show as empty strings
- raw shows the raw data in a cell array
numerical values are vectors - To write the data back to a spread sheet, use
- xlswrite(ltfilenamegt, ltarraygt, ltsheetgt, ltrangegt)
15Text File Vocabulary
- A text file consists of many lines, each line is
separated from the next by an invisible newline
character - PCs, Macs and Unix/Linux all use different
newline chars - PCs use carriage return linefeed or CR/LF
- Unix systems use LF and Macs use CR
- Each line (or record) can be subdivided into a
series of fields or columns separated by a
field delimiter. - Different programs use different delimiters
(e.g., spaces, commas, tabs, quotes, etc.) - Numbers, etc. are actually stored in ASCII (or
Unicode) not binary, and must be translated back
and forth.
16Delimited Text
- Sometimes, data are provided in text form where
the columns are separated (delimited) by commas,
spaces, or tab characters. - Rows in the data are separated as expected by new
lines. - As long as the data are numerical only,
- dlmread(ltfilegt, ltdelimitergt)
- will read the file into an array.
- The delimiter parameter is a single, optional
character that can be used to specify an unusual
delimiting character. However, the function can
usually determine common delimiter situations
without specifying the delimiter. - Notice that the array elements where data is not
supplied are filled with zero. - To write a delimited file, use
- dlmwrite(ltfilegt, ltarraygt, ltdelimitergt)
- If the delimiter is not specified, it is presumed
to be a comma.
17Working with Numeric Data Files
- It is common to encounter files that contain
columns and rows of numeric data - numbers have different precisions and formats
- delimiters (spaces, tabs, ) separate columns
Command dlmread( ) - read data from a text
file Examples
Specify a delimiter
gtgt data dlmread('demo.txt',' ') gtgt data1
dlmread('demo1.dat','') gtgt help dlmread
------ YR MO DY ---- HR HGLOBL HDIR ----- -----
----- -- DOKDOK 81 7 8 016 1 .0 0
0 2 0 . DOKDOK 81 7 8 116 2 .0
0 0 0 0 . DOKDOK 81 7 8 216
3 .0 0 0 0 0 . DOKDOK 81 7
8 316 4 .0 0 0 0 0
. DOKDOK 81 7 8 416 5 .0 0 0
0 0 . SOKSOK 81 7 8 516 6 47.9 0
1 0 29 . SOKSOK 81 7 8 616 7
776.4 937 149 156 343 . SOKSOK 81 7 8
716 8 1729.5 1892 264 576 969 . SOKSOK
81 7 8 816 9 2632.9 2355 358 1275 1668
. SOKSOK 81 7 8 916 10 3425.2 2634 427
2006 2351 . SOKSOK 81 7 8 1016 11 4052.3
2546 610 2550 2796 . SOKSOK 81 7 8 1116 12
4471.4 1696 892 2406 2530 . SOKSOK 81 7
8 1216 13 4654.1 2128 1055 3071 3177
. SOKSOK 81 7 8 1316 14 4587.7 1771 1149
2814 2905 . SOKSOK 81 7 8 1416 15 4276.9
2337 932 2945 3062 .
18Text File Vocabulary contd
- Here is a sample text file with space delimiters
1.0000000e001 5.0000000e000 -2.3465600e000
1.0000000e001 5.2000000e000 -2.3658827e000
1.0000000e001 5.4000000e000 -2.3559947e000
1.0000000e001 5.6000000e000 -2.3716188e000
1.0000000e001 5.8000000e000 -2.3921178e000
- Here is a sample text file with comma delimiters
10.000, 5.0000, -2.3465600e000 10.000,
5.2000, -2.3658827e000 10.000, 5.4000,
-2.3559947e000 10.000, 5.6000,
-2.3716188e000 10.000, 5.8000, -2.3921178e000
19Other File Formats MATLAB Can Handle
- There are a number of other common data file
formats that can contain useful information - comma separated variables (.csv files from Excel)
- image files (.gif, .jpeg, .tiff, etc) more in
week 10 - movie (.avi)
- sound (.wav, .au )
- MATLAB can import data in many of these formats.
- See help fileformats for a comprehensive list of
file formats that can be read with high level
functions.
20File Input and Output
21Lower-Level File I/O
- Some text files contain data in mixed format that
are not readable by the high-level file reading
functions. MATLAB provides a set of lower-level
I/O functions that permit general purpose text
file reading and writing. - Opening and Closing Files
- Reading Text Files
- Examples of Reading Text Files
- Writing Text Files
- It is also possible to read and write binary
files, but that is not addressed in this class.
22MATLAB File Input
- If the file is ASCII but not a table, file I/O
needs fopen and fclose - fgets reads a line as a string (from a file)
- fprintf writes formatted data (to a file)
23Opening Files
- To read a file manually, open with fopen
- fidfopen('fname.txt', 'rt')
- fid will be lt1 if open fails
- File I/O functions accept fid
- Close the file when youre done with fclose(fid)
24 Low-level File I/O Functions
- fid fopen('file.name', permission)
- Opens file.name and return the file identifier
fid.The file is opened for reading only (mode
'r').If the file does not exist, a -1 is
returned. - Some modes of opening if permission is specified
are - 'r' read
- 'w' write (create if necessary)
- 'a' append (create if necessary)
- 'r' read and write (do not create)
- Add 't' to the permission string for a text file
- fclose(fid) closes the file with identifier fid.
25Opening Files
- Examples
- fid fopen( 'example.dat', 'rt' )opens a text
file for input - fid fopen( 'example.dat', 'wt' )opens a text
file for output (if example.dat already exists,
it will be deleted) - fid fopen( 'example.dat', 'at' )opens a text
file for output (if example.dat already exists,
new data will be appended to the end)
26Closing Files
- status fclose( fid )closes the file with file
id fid - If the closing operation is successful, status
will be 0 - If the closing operation is unsuccessful, status
will be -1 - status fclose( 'all' )closes all open files
(except for standard output and standard error)
27Reading Text Files a Line at a Time
- To read a whole line including the new line
character, use - str fgets( fid )
- which will return each line as a string until the
end of the file, when the value 1 is returned
instead of a string. - To leave out each new-line character, use
- fgetl(...)
- (the last character is a lowercase L).
28Hands-On DEMO Read from the Gettysburg Address
- format compact
- fid fopen('gettysburg.txt','r')
- line1 fgets(fid)
- line2 fgets(fid)
- line3 fgets(fid)
- fclose(fid)
- disp(line3) displays first 3 lines
- disp(line2) in reverse order
- disp(line1) WHY? you may ask...
29Reading Formatted Text Data
- textscan reads formatted data from text file.
- C textscan(fid, format) reads data from the
text file identified by fid into cell array C. - The data are parsed into fields and converted
according to the conversions specified in the
string format. - The number of cells in C will equal the number of
conversion specifiers in format. - The type of each cell is determined by format.
30Hands-On DEMO textscan (studs.m)
- studs.txt
- Anne 3.90
- Barbara 3.54
- Carl 2.98
- fid fopen('studs.txt')
- stud textscan(fid, 's f')
- fclose(fid)
- names stud1 still cells (lengths differ)
- GPAs stud2
31Writing Text Files
- Once a file has been opened for writing, the
fprintf(...) function can be used to write to it
by including its file handle as the first
parameter. - For example, this code copies a text file
- ifn input( 'input file name ', 's' )
- ofn input('output file name ', 's' )
- ih fopen( ifn, 'r' )
- oh fopen( ofn, 'w' )
- ln ''
- while true
- ln fgets( ih )
- if ischar( ln )
- break
- end
- fprintf( oh, ln )
- end
- fclose( ih )
- fclose( oh )
32Writing Formatted Text Data
- count fprintf(fid,format,val1,val2,)writes
formatted text data in a user-specified format - fid file id (if fid is missing, data is written
to the standard output device (command window) - format same as what we have been using
(combination of format specifiers that start with
) - count number of characters written
33Writing Files
- Save matrices using save fname varname -ascii
- Doing it yourself
- fidfopen('fname',wt)
- fprintf(fid, cstring, variables)
- Example
- A(110)', sin(2pi0.1(110)')
- fidfopen('example.txt', 'wt')
- fprintf(fid, 'd f\n', A')
- fclose(fid)
34DEMO Formatted Text I/O (1 of 3)
- Script file table1.m
- Purpose To create a table of square roots,
squares, and cubes. - Double-click table.dat in Current
Directory to verify! - Open the file.
- fid fopen('table.dat', 'wt')
- Print the title of the table.
- fprintf(fid, ' Table of Square Roots, Squares,
and Cubes\n\n') - Print column headings
- fprintf(fid, ' Number Square Root Square
Cube\n') - fprintf(fid, '
\n') - Generate the required data
- ii 110
- square_root sqrt(ii)
- square ii.2
- cube ii.3
35DEMO Formatted Text I/O (2 of 3)
- Script file table2.m
- Updates the phone number of a person
- Double-click phones2.txt to verify
- Get the name and new phone number
- name input( 'Enter the last name of the person
', 's' ) - new_phone input( 'Enter the new phone number '
) - Read the phone numbers
- fid fopen('phones.txt')
- directory textscan( fid, 's s s d' )
- fnames directory1 fnames is cell array of
strings (lengths differ) - lnames directory2 lnames is cell array of
strings (note in fprintf below) - ranks directory3 ranks is cell array of
strings - phones directory4 phones is normal array
of numbers - fclose(fid)
- Find the person and update the phone number
- for i 1length(lnames)
- if ( strcmp( lnames(i), name ) )
- phones(i) new_phone
- end
36DEMO Formatted Text I/O (3 of 3)
- Script file table3.m
- Updates the name of a person
- Double-click phones3.txt to verify
- Get the old and new names
- old_name input( 'Enter the old name ', 's' )
- new_name input( 'Enter the new name ', 's' )
- Open the input file
- fid1 fopen( 'phones.txt', 'rt' )
- Open the output file
- fid2 fopen( 'phones3.txt', 'wt' )
- Read lines one by one
- while true
- line fgets( fid1 )
- if line lt 0
- break
- end
- Replace the old name with the new name
37In-Class Exercise 6c Temperature Extremes
- Use textscan to read in data from the file
temps.txt containing city name, month number, and
the average high low temperature. - Use input format ' s d f f '
- Display the
- City month with lowest average low.
- City month with highest average high.
- City month with highest average low.
- City month with lowest average high.
- Should get
- Denver had highest average temp (88) in month 7
- Denver had lowest average temp (15) in month 1
- Chicago had highest average low temp (66) in
month 7 - Chicago had lowest average high temp (32) in
month 1
38END