Your choice of function depends on how the
data in the text file is formatted.
The text data must be formatted in a uniform pattern of rows and columns,
using a text character, called a delimiter or column separator, to separate each
data item. The delimiter can be space, comma, semicolon, tab, or any other
character. The individual data items can be alphabetic or numeric characters
or a mix of both.
The text file may also contain one or more lines of text, called header lines, or
may use text headers to label each column or row. The following example
illustrates a tab-delimited text file with header text and row and column
headers.
Text header line
Class Grades for Spring Term
Column Headers
Grade1 Grade2 Grade3
John 85
90
95
Row Headers
Ann 90
92
98
Martin 100
95
97
Rob
77
86
93
Tab-delimited data
To find out how your data is formatted, view it in a text editor. After you
determine the format, scan the data format samples in Table 6-1 and look for
the sample that most closely resembles the format of your data. Read the topic
referred to in the table for more information.
6-9
6 Importing and Exporting Data
Table 6-1: ASCII Data File Formats and MATLAB Import Commands
Data Format Sample
File
Description
Extension
1 2 3 4 5
.txt
See “Importing Numeric Text Data” on page 6-11 for
6 7 8 9 10
.dat
more information. You can also use the Import
or other
Wizard for this data format. See “Using the Import
Wizard with Text Data” on page 6-4 for more
information.
1; 2; 3; 4; 5
.txt
See “Importing Delimited ASCII Data Files” on page
6; 7; 8; 9; 10
.dat
6-12 for more information. You can also use the
or
.csv
Import Wizard for this data format. See “Using the
1, 2, 3, 4, 5
or other
Import Wizard with Text Data” on page 6-4 for more
6, 7, 8, 9, 10
information.
Ann Type1 12.34 45 Yes
.txt
See “Importing Numeric Data with Text Headers”
Joe Type2 45.67 67 No
.dat
on page 6-13 for more information.
or other
Grade1 Grade2 Grade3
.txt
See “Importing Numeric Data with Text Headers”
91.5
89.2
77.3
.dat
on page 6-13 for more information. You can also use
88.0
67.8
91.0
or other
the Import Wizard for this data format. See “Using
67.3
78.1
92.5
the Import Wizard with Text Data” on page 6-4 for
more information.
If you are familiar with MATLAB import functions but not sure when to use
them, view Table 6-2 which compares the features of each function.
6-10
Importing Text Data
Table 6-2: ASCII Data Import Function Feature Comparison
Function
Data Type
Delimiters
Number of
Notes
Return
Values
csvread
Numeric data
Only
One
Primarily used with
commas
spreadsheet data. See
also the binary format
spreadsheet import
functions.
dlmread
Numeric data
Any
One
Flexible and easy to use.
character
fscanf
Alphabetic and
Any
One
Part of low-level file I/O
numeric;
character
routines. Requires use of
however, both
fopen to obtain file
types returned
identifier and fclose
in a single
after read.
return variable
load
Numeric data
Only spaces
One
Easy to use. Use the
functional form of load
to specify the name of
the output variable.
textread
Alphabetic and
Any
Multiple
Flexible, powerful, and
numeric
character
return
easy to use. Use format
values.
string to specify
conversions.
Importing Numeric Text Data
If your data file contains only numeric data, you can use many of the MATLAB
import functions (listed in Table 6-2), depending on how the data is delimited.
If the data is rectangular, that is, each row has the same number of elements,
the simplest command to use is the load command. (The load command can
also be used to import MAT-files, MATLAB’s binary format for saving the
workspace.)
6-11
6 Importing and Exporting Data
For example, the file named my_data.txt contains two rows of numbers
delimited by space characters.
1 2 3 4 5
6 7 8 9 10
When you use load as a command, it imports the data and creates a variable
in the workspace with the same name as the filename, minus the file extension.
load my_data.txt;
whos
Name
Size
Bytes
Class
my_data
2x5
80
double array
my_data
my_data =
1
2
3
4
5
6
7
8
9
10
If you want to name the workspace variable something other than the file
name, use the functional form of load. In the following example, the data from
my_data.txt is loaded into the workspace variable A.
A = load('my_data.txt');
Importing Delimited ASCII Data Files
If your data file uses a character other than a space as a delimiter, you have a
choice of several import functions you can use. (See Table 6-4 for a complete
list.) The simplest to use is the dlmread function.
For example, consider a file named ph.dat whose contents are separated by
semicolons.
7.2;8.5;6.2;6.6
5.4;9.2;8.1;7.2
To read the entire contents of this file into an array named A, enter
A = dlmread('ph.dat', ';');
6-12
Importing Text Data
You specify the delimiter used in the data file as the second argument to
dlmread. Note that, even though the last items in each row are not followed by
a delimiter, dlmread can still process the file correctly. dlmread ignores space
characters between data elements. So, the preceding dlmread command works
even if the contents of ph.dat are
7.2; 8.5; 6.2;6.6
5.4; 9.2 ;8.1;7.2
Importing Numeric Data with Text Headers
To import an ASCII data file that contains text headers, use the textread
function, specifying the headerlines parameter. textread accepts a set of
predefined parameters that control various aspects of the conversion. (For a
complete list of these parameters, see the textread reference page.) The
headerlines parameter lets you specify the number of lines at the head of the
file that textread should ignore.
For example, the file, grades.dat, contains formatted numeric data with a
one-line text header.
Grade1 Grade2 Grade3
78.8 55.9
45.9
99.5
66.8
78.0
89.5
77.0
56.7
To import this data, use this command:
|