Matlab File & Directory Management

16
Fall 2006 AE6382 Design Computing 1 Matlab File & Directory Management Learning Objectives Define file input and output terminology Compare high and low level file operations Explore spreadsheet I/O Explore text file I/O • Topics Brief overview then Lots and lots of exercises! CHAPTER 14 - Mastering MATLAB

description

Matlab File & Directory Management. Learning Objectives Define file input and output terminology Compare high and low level file operations Explore spreadsheet I/O Explore text file I/O. Topics Brief overview then Lots and lots of exercises! CHAPTER 14 - Mastering MATLAB. - PowerPoint PPT Presentation

Transcript of Matlab File & Directory Management

Page 1: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 1

Matlab File & Directory Management

Learning Objectives

•Define file input and output terminology

•Compare high and low level file operations

•Explore spreadsheet I/O

•Explore text file I/O

• Topics

• Brief overview then

• Lots and lots of exercises!

• CHAPTER 14 - Mastering MATLAB

Page 2: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 2

File Input and Output

• Reading from a source

Writing to a destination

Page 3: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 3

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.

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 …

>>save demo1 or >>load demo1

>>help save or >>help load

Page 4: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 4

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

Page 5: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 5

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.

• MATLAB provides numerous functions to help in working with these different formats.

• REFER to section 13.2 (pp. 206)

>> help fileformats

Page 6: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 6

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

Page 7: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 7

Working with Spreadsheet Data

• Command:• xlsread - read data from an Excel

spreadsheet• Examples:

• 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

>> m = xlsread(‘demo.xls’);

>> [a,b] = xlsread(‘demo.xls’);

>> help xlsread

Page 8: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 8

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:

• What does this code do? Can you embed this code within a function to display contents of an “m-file?”

>> j = fopen(‘proj3data.txt’);

>> while (not(feof(j)))

line = fgetl(j);

disp(line);

>> end;

>> fclose(j);

NOTE: See help iofun for a description of the different I/O functions

NOTE: See help iofun for a description of the different I/O functions

Page 9: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 9

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')endif isempty(filename) error('LISTFILE: argument not defined')endfid=fopen(filename,'rt'); % open as text file for read onlyif fid==-1 error('LISTFILE: file not found or can''t be opened')endwhile not(feof(fid)) line = fgetl(fid); disp(line);end;fclose(fid);

Page 10: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 10

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.

Page 11: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 11

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

Page 12: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 12

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

Page 13: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 13

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?

Page 14: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 14

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.

Page 15: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 15

Summary

Action Items

• Review the lecture

• Use this to complete homework

• Review questions

• Describe basic concepts of file I/O.

• Compare and contrast high- versus low-level file I/O.

• Identify important elements of a text file

• Why use text files?

Page 16: Matlab File & Directory Management

Fall 2006AE6382 Design Computing 16

Important commands

• >> help iofun• >> help fileformats• >> help xlsread• >> help dlmread• >> help sprintf