| Programming | ![]() |
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:
| Note To read an ASCII data file that contains numeric data with text column headers, see Importing Numeric Data with Text Headers. |
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:
[names, types, x, y, answer] = ... textread('mydata.dat', '%s %s %f %d %s', 3) names = 'Sally' 'Larry' 'Tommy' types = 'Type1' 'Type2' 'Type1' x = 12.3400 34.5600 67.8900 y = 45 54 23 answer = 'Yes' 'Yes' 'No'
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.
| Importing Numeric Data with Text Headers | Importing from XML Documents | ![]() |
© 1994-2005 The MathWorks, Inc.