File and directory

Download Report

Transcript File and directory

Matlab File & Directory Management
Learning Objectives
• Topics
•Define file input and
output terminology
• Brief overview then
•Compare high and low
level file operations
• Lots and lots of exercises!
•Explore spreadsheet I/O
•Explore text file I/O
AE6382 Design Computing
• CHAPTER 14 - Mastering
MATLAB
1
Fall 2006
File Input and Output
• Reading from a source
Writing to a destination
AE6382 Design Computing
2
Fall 2006
High Level File Input and Output
• The highest (most abstract) level of file I/O operations
works with the entire MATLAB workspace, or with
individual variables.
>>save demo1
>>help save
or
or
>>load demo1
>>help load
EXERCISE:
•Review the HELP descriptions for save and load
•Create several variables
•Save them to disk
•Clear memory
•Load one of the variables
•Discuss some uses of these statements …
AE6382 Design Computing
3
Fall 2006
Features of High Level I/O
• The programmer thinks about save and retrieving entire
sets of variables
• The programmer does not consider how the individual
bytes are stored on disk
• The programmer does not consider how the variables
are organized within the file
• There is not explicit “open” or “close” of the file stream,
one simply “grabs” entire sets of variables from a file (or
copies them to files).
AE6382 Design Computing
4
Fall 2006
Data Import and Export
• The next highest level of file I/O describing working with
entire files, but whose contents are not explicitly
MATLAB variables.
• For example, working with spreadsheets, images, audio
and video files, or raw ASCII text.
>> help fileformats
• MATLAB provides numerous functions to help in working
with these different formats.
• REFER to section 13.2 (pp. 206)
AE6382 Design Computing
5
Fall 2006
Working with Text 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:
>> data = dlmread(‘demo.txt’,‘ ‘);
>> [a,b] = dmsread(‘demo1.dat’,‘;’);
>> help dlmread
AE6382 Design Computing
6
Fall 2006
Working with Spreadsheet Data
• Command:
•
xlsread - read data from an Excel spreadsheet
• Examples:
>> m = xlsread(‘demo.xls’);
>> [a,b] = xlsread(‘demo.xls’);
>> help xlsread
• Exercise:
• Download example spreadsheet (DEMO.XLS) from
web site
• Explore various forms of xlsread command
• How are data stored in MATLAB? What about
column and row names? What about the formulas?
• Discuss how this routine can be incorporated into
your home work assignment
AE6382 Design Computing
7
Fall 2006
Low Level File I/O
• At the lowest level, the programmer is responsible for
the moving the the bits and bytes.
• File streams are “opened” and “closed”
• Here is typical MATLAB code:
>> j = fopen(‘proj3data.txt’);
>> while (not(feof(j)))
line = fgetl(j);
disp(line);
>> end;
NOTE:
See help iofun for a
description of the
different I/O functions
>> fclose(j);
• What does this code do? Can you embed this code
within a function to display contents of an “m-file?”
AE6382 Design Computing
8
Fall 2006
Low Level File I/O (2)
• Doing low level file I/O can get very tricky so you will
have to read the documentation carefully if you use this
approach!
function listfile(filename)
% demo of low level file I/O
if ~ischar(filename)
error('LISTFILE: argument must be filename')
end
if isempty(filename)
error('LISTFILE: argument not defined')
end
fid=fopen(filename,'rt'); % open as text file for read only
if fid==-1
error('LISTFILE: file not found or can''t be opened')
end
while not(feof(fid))
line = fgetl(fid);
disp(line);
end;
fclose(fid);
AE6382 Design Computing
9
Fall 2006
Text File Vocabulary
• A file consists of many lines, each line is separated from the next by
an invisible “newline” character
– PC’s, Mac’s and Unix/Linux all use different newline chars
– PC’s use “carriage return & linefeed” or CR/LF
– Unix systems use LF and Mac’s 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 not binary, and must be
translated back and forth.
AE6382 Design Computing
10
Fall 2006
Text file vocabulary
• Here is a sample from our text file:
1.0000000e+001
5.0000000e+000 -2.3465600e+000
1.0000000e+001
5.2000000e+000 -2.3658827e+000
1.0000000e+001
5.4000000e+000 -2.3559947e+000
1.0000000e+001
5.6000000e+000 -2.3716188e+000
1.0000000e+001
5.8000000e+000 -2.3921178e+000
AE6382 Design Computing
11
Fall 2006
Encoding information on the computer
• ASCII
• American Standard Code for Information Interchange
• 1 character  1 byte
•
•
•
•
Unicode
the new standard
1 character  2-4 bytes
Allows representing every character in a language
(Chinese included!)
AE6382 Design Computing
12
Fall 2006
Exercise: working with text files
• Download text files ttimes.txt and atlanta.txt
from web site.
• Try loading the data using various commands
– load
– dlmread
– Import to a spreadsheet, then use xlsread into Matlab
• In atlanta.txt, each record represents a single line,
with the 3,4,5 and 6 columns represent X1,Y1, X2 and
Y2 points for that line.
• Write a program that plots each of these lines. What
does the picture represent?
AE6382 Design Computing
13
Fall 2006
Optional exercise: working with text files
• The file ttimes.txt contains six columns:
•
•
•
•
Row Id, Col Id, Point ID, X, Y, TT
Load the file,
View the first 10 rows - what do you notice?
Enter >>format short g then view the data again
• Write a script to create a “PLAID” from X, Y and TT. The
RowID and ColID can be used to position these data into
a 25x25 matrix.
• Create a contour plot using the plaid
• Modify your script to overlay the lines (from
atlanta.txt) onto the contour.
AE6382 Design Computing
14
Fall 2006
Summary
• Review questions
Action Items
• Describe basic concepts of
file I/O.
• Review the lecture
• Compare and contrast highversus low-level file I/O.
• Use this to complete
homework
• Identify important elements
of a text file
• Why use text files?
AE6382 Design Computing
15
Fall 2006
Important commands
•
•
•
•
•
>>
>>
>>
>>
>>
help
help
help
help
help
iofun
fileformats
xlsread
dlmread
sprintf
AE6382 Design Computing
16
Fall 2006