1 / 13

CMPS 1371 Introduction to Computing for Engineers

FILE Input / Output. CMPS 1371 Introduction to Computing for Engineers. File Input and Output. Reading from a source. Writing to a destination. Reading and Writing Data from Files. Some common types of data files are mat dat txt xls jpg. Load and Save commands.

enya
Download Presentation

CMPS 1371 Introduction to Computing for Engineers

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. FILE Input / Output CMPS 1371Introduction to Computing for Engineers

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

  3. Reading and Writing Data from Files • Some common types of data files are • mat • dat • txt • xls • jpg

  4. Load and Save commands The highest (most abstract) level of file I/O operations works with the entire MATLAB workspace, or with individual variables. >>save demo1 or >>load demo1 >>help save or >>help load

  5. Save and Load Let’s try this Review the HELP descriptions for save and load. Create several variables, Save them to disk, Clear memory, Load one of the variables

  6. 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. • MATLAB provides numerous functions to help in working with these different formats. >> help fileformats

  7. 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 Space delimiter Comma delimiter 1 5.00 -2.3465600e+000 1 5.20 -2.3658827e+000 1 5.40 -2.3559947e+000 1 5.60 -2.3716188e+000 1 5.80 -2.3921178e+000 1, 5.00, -2.3465600e+000 1, 5.20, -2.3658827e+000 1, 5.40, -2.3559947e+000 1, 5.60, -2.3716188e+000 1, 5.80, -2.3921178e+000

  8. Numeric Data Files Command: dlmread( ) - read data from a text file Examples: Specify a delimiter >> data = dlmread(‘file.txt’,‘ ’); >> data1= dlmread(‘file.dat’,‘;’,range); >> help dlmread YR MO DAY ID HRS RATE PAY 2007 9 14 145921 5 $6.50 $32.50 2007 9 14 146250 2 $5.25 $10.50 2007 9 14 146318 9 $8.75 $78.75 2007 9 14 146822 6 $7.25 $43.50 2007 9 14 146903 6 $6.25 $37.50 2007 9 14 147002 3 $9.00 $27.00 2007 9 14 147218 4 $6.75 $27.00 2007 9 14 147445 7 $7.00 $49.00 2007 9 14 147496 5 $8.50 $42.50 Specify a range of data within file to read

  9. Text Data Files When the data file contains text, dlmread( ) cannot handle it properly One solution is to use importdata( ) which is described in the fileformats help info. importdata( ) will separate the text columns and the numeric columns into separate fields in a structure. The contents of the structure can be copied into numeric variables for analysis. >> a = importdata(‘file.txt’,‘ ’,headerline); >>

  10. Spreadsheet Data • xlsread( ) - read data from an Excel spreadsheet • Let’s try: • Create a spreadsheet with Excel, • Explore various forms of xlsread( ) function, • How are data stored in MATLAB? What about column and row names? What about the formulas? • Check out the xlsfinfo( ) function >> m = xlsread(‘file.xls’); >> [a,b] = xlsread(‘file.xls’); >> help xlsread Numbers in a and text in b

  11. Import Wizard Use the import wizard to determine the data type and to suggest ways to represent the data Launch from the file menu

  12. Exporting Data Use the save function for .mat or .dat files Use specialized functions for other file types For example xlswrite for Excel files

  13. Importing and Exporting Data • MATLAB includes a number of specialized import functions optimized for a variety of file formats • Companion functions allow data to be exported in the same file formats

More Related