Development Environment    

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

 

Table 6-1: ASCII Data File Formats and MATLAB Import Commands  
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 for more information. You can also use the Import Wizard for this data format. See Using the Import Wizard with Text Data for more 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 for more information. You can also use the Import Wizard for this data format. See Using the Import Wizard with Text Data for more 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 more 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 for more information. You can also use the Import Wizard for this data format. See Using the Import Wizard with Text Data for more information.

If you are familiar with MATLAB import functions but are not sure when to use them, view Table 6-2, which compares the features of each function.

Table 6-2: ASCII Data Import Function Feature Comparison

Function
Data Type
Delimiters
Number of Return Values
Notes
csvread
Numeric data
Only commas
One
Primarily used with spreadsheet data. See also the binary format spreadsheet import functions.
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
Only spaces
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 return values.
Flexible, powerful, and easy to use. Use format string to specify conversions.

  Using the Import Wizard with Text Data Importing Numeric Text Data