1 / 16

Matlab File & Directory Management

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.

lara-mays
Download Presentation

Matlab File & Directory Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 AE6382 Design Computing

  2. Reading from a source File Input and Output Writing to a destination AE6382 Design Computing

  3. The highest (most abstract) level of file I/O operations works with the entire MATLAB workspace, or with individual variables. High Level File Input and Output >>save demo1 or >>load demo1 >>help save or >>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

  4. 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). Features of High Level I/O AE6382 Design Computing

  5. 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) Data Import and Export >> help fileformats AE6382 Design Computing

  6. 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 Working with Text Files Command: dlmread - read data from a text file Examples: >> data = dlmread(‘demo.txt’,‘ ‘); >> [a,b] = dmsread(‘demo1.dat’,‘;’); >> help dlmread AE6382 Design Computing

  7. 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 Working with Spreadsheet Data >> m = xlsread(‘demo.xls’); >> [a,b] = xlsread(‘demo.xls’); >> help xlsread AE6382 Design Computing

  8. 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?” Low Level File I/O >> 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 AE6382 Design Computing

  9. Doing low level file I/O can get very tricky so you will have to read the documentation carefully if you use this approach! Low Level File I/O (2) 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

  10. 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. Text File Vocabulary AE6382 Design Computing

  11. Here is a sample from our text file: Text file vocabulary 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

  12. ASCII American Standard Code forInformationInterchange 1 character  1 byte Unicode the new standard 1 character  2-4 bytes Allows representing every character in a language (Chinese included!) Encoding information on the computer AE6382 Design Computing

  13. 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? Exercise: working with text files AE6382 Design Computing

  14. 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. Optional exercise: working with text files AE6382 Design Computing

  15. 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? Summary • Action Items • Review the lecture • Use this to complete homework AE6382 Design Computing

  16. >> help iofun >> help fileformats >> help xlsread >> help dlmread >> help sprintf Important commands AE6382 Design Computing

More Related