Development Environment |
Importing Mixed Alphabetic and Numeric Data
If your data file contains a mix of alphabetic and numeric ASCII data, use the textread
function to import the data. textread
can return multiple output variables, and you can specify the data type of each variable.
For example, the file mydata.dat
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',1) 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 | Exporting ASCII Data |