Download presentation
Presentation is loading. Please wait.
Published byAngel Maxwell Modified over 9 years ago
1
Fall 2006AE6382 Design Computing1 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
2
Fall 2006AE6382 Design Computing2 File Input and Output Reading from a source Writing to a destination
3
Fall 2006AE6382 Design Computing3 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
4
Fall 2006AE6382 Design Computing4 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).
5
Fall 2006AE6382 Design Computing5 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
6
Fall 2006AE6382 Design Computing6 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
7
Fall 2006AE6382 Design Computing7 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
8
Fall 2006AE6382 Design Computing8 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
9
Fall 2006AE6382 Design Computing9 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);
10
Fall 2006AE6382 Design Computing10 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.
11
Fall 2006AE6382 Design Computing11 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
12
Fall 2006AE6382 Design Computing12 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!)
13
Fall 2006AE6382 Design Computing13 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?
14
Fall 2006AE6382 Design Computing14 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.
15
Fall 2006AE6382 Design Computing15 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?
16
Fall 2006AE6382 Design Computing16 Important commands >> help iofun >> help fileformats >> help xlsread >> help dlmread >> help sprintf
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.