Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPS 1371 Introduction to Computing for Engineers FILE Input / Output.

Similar presentations


Presentation on theme: "CMPS 1371 Introduction to Computing for Engineers FILE Input / Output."— Presentation transcript:

1 CMPS 1371 Introduction to Computing for Engineers FILE Input / Output

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 delimiterComma 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: >> data = dlmread(‘file.txt’,‘ ’); >> data1= dlmread(‘file.dat’,‘;’,range); >> help dlmread Specify a range of data within file to read Specify a delimiter YRMODAYID HRSRATE PAY 20079141459215$6.50$32.50 20079141462502$5.25$10.50 20079141463189$8.75$78.75 20079141468226$7.25$43.50 20079141469036$6.25$37.50 20079141470023$9.00$27.00 20079141472184$6.75$27.00 20079141474457$7.00$49.00 20079141474965$8.50$42.50

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


Download ppt "CMPS 1371 Introduction to Computing for Engineers FILE Input / Output."

Similar presentations


Ads by Google