Programming Previous page   Next Page

Importing Mixed Alphabetic and Numeric Data

If your data file contains a mix of alphabetic and numeric ASCII data, use the textscan or textread function to import the data. textscan returns its output in a single cell array, while textread returns its output in separate variables and you can specify the data type of each variable. The textscan function offers better performance than textread, making it a better choice when reading large files.

This example uses textread to import the file mydata.dat that contains a mix of alphabetic and numeric data:

To read the entire contents of the file mydata.dat into the workspace, specify the name of the data file and the format string as arguments to textread. In the format string, you include conversion specifiers that define how you want each data item to be interpreted. For example, specify %s for string data, %f for floating-point data, and so on. (For a complete list of format specifiers, see the textread reference page.)

For each conversion specifier in your format string, you must specify a separate output variable. textread processes each data item in the file as specified in the format string and puts the value in the output variable. The number of output variables must match the number of conversion specifiers in the format string.

In this example, textread reads the file mydata.dat, applying the format string to each line in the file until the end of the file:

If your data uses a character other than a space as a delimiter, you must use the textread parameter 'delimiter' to specify the delimiter. For example, if the file mydata.dat used a semicolon as a delimiter, you would use this command:

See the textread reference page for more information about these optional parameters.


Previous page  Importing Numeric Data with Text Headers Importing from XML Documents Next page

© 1994-2005 The MathWorks, Inc.