data = dlmread(`file.txt`,` `)

Download Report

Transcript data = dlmread(`file.txt`,` `)

CMPS 1371 Introduction to Computing for Engineers

FILE Input / Output

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

• 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

 Let’s try this

Save and Load

 Review the HELP descriptions for save and load.

 Create several variables,  Save them to disk,  Clear memory,  Load one of the variables

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

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

Numeric Data Files

Command: dlmread( )

- read data from a text file

Specify a delimiter Examples: >> data = dlmread (‘file.txt’,‘ ’); >> data1= dlmread (‘file.dat’,‘;’,range); >> help dlmread

YR MO 2007 9 2007 9 2007 9 2007 9 2007 9 2007 9 2007 9 2007 9 2007 9 DAY 14 14 14 14 14 14 14 14 14 ID 145921 146250 146318 146822 146903 147002 147218 147445 147496 HRS 5 2 9 6 6 3 4 7 5 RATE $6.50

$5.25

$8.75

$7.25

$6.25

$9.00

$6.75

$7.00

$8.50

PAY $32.50

$10.50

$78.75

$43.50

$37.50

$27.00

$27.00

$49.00

$42.50

Specify a range of data within file to read

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

Spreadsheet Data

 xlsread( )

- read data from an Excel spreadsheet >> m = xlsread(‘file.xls’); >> [a,b] = xlsread(‘file.xls’); >> help xlsread Numbers in a and text in b

 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

Import Wizard

 Use the import wizard to determine the data type and to suggest ways to represent the data  Launch from the file menu

Exporting Data

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

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