Programming Previous page   Next Page

Using Import Functions with Text Data

To import text data from the command line or in an M-file, you must use one of the MATLAB import functions. 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 a 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 can also contain one or more lines of text, called header lines, or can 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.

To find out how your data is formatted, view it in a text editor. After you determine the format, find the sample in the table below that most closely resembles the format of your data. Then read the topic referred to in the table for information on how to import that format. 

Table 6-1: ASCII Data File Formats  
Data Format Sample
File Extension
Description
1 2 3 4 5
6 7 8 9 10

.txt
.dat
or other
See Importing Numeric Text Data or Using the Import Wizard with Text Data for information.
1; 2; 3; 4; 5
6; 7; 8; 9; 10
or
1, 2, 3, 4, 5
6, 7, 8, 9, 10

.txt
.dat
.csv
or other
See Importing Delimited ASCII Data Files or Using the Import Wizard with Text Data for information.
Ann Type1 12.34 45 Yes
Joe Type2 45.67 67 No

.txt
.dat
or other
See Importing Numeric Data with Text Headers for information.
Grade1 Grade2 Grade3
91.5   89.2   77.3
88.0   67.8   91.0
67.3    78.1   92.5

.txt
.dat
or other
See Importing Numeric Data with Text Headers or Using the Import Wizard with Text Data for information.

If you are familiar with MATLAB import functions but are not sure when to use them, see the following table, which compares the features of each function.

Table 6-2: ASCII Data Import Function Features  
Function
Data Type
Delimiters
Number of Return Values
Notes
csvread
Numeric data
Commas only
One
Primarily used with spreadsheet data. See Working with Spreadsheets.
dlmread
Numeric data
Any character
One
Flexible and easy to use.
fscanf
Alphabetic and numeric; however, both types returned in a single return variable
Any character
One
Part of low-level file I/O routines. Requires use of fopen to obtain file identifier and fclose after read.
load
Numeric data
Spaces only
One
Easy to use. Use the functional form of load to specify the name of the output variable.
textread
Alphabetic and numeric
Any character
Multiple values in cell arrays
Flexible, powerful, and easy to use. Use format string to specify conversions.
textscan
Alphabetic and numeric
Any character
Multiple values returned to one cell array
More flexible than textread. Also more format options.


Previous page  Importing Text Data Importing Numeric Text Data Next page

© 1994-2005 The MathWorks, Inc.