23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Importing Text Data<br />

66.8000<br />

77.0000<br />

ans =<br />

45.9000<br />

78.0000<br />

56.7000<br />

fclose(fid);<br />

Importing Mixed Alphabetic and Numeric Data<br />

If your data file contains a mix of alphabetic and numeric ASCII data, use<br />

the textscan or textread function to import the data. textscan returns its<br />

output in a single cell array, while textread returns its output in separate<br />

variables and you can specify the data type of each variable. The textscan<br />

function offers better performance than textread, making it a better choice<br />

when reading large files.<br />

This example uses textread to import the file mydata.dat that contains a<br />

mix of alphabetic and numeric data:<br />

Sally<br />

Larry<br />

Tommy<br />

Type1 12.34 45 Yes<br />

Type2 34.56 54 Yes<br />

Type1 67.89 23 No<br />

Note To read an ASCII data file that contains numeric data with text column<br />

headers, see “Importing Numeric Data with Text Headers” on page 6-80.<br />

To read the entire contents of the file mydata.dat into the workspace, specify<br />

the name of the data file and the format string as arguments to textread. In<br />

the format string, you include conversion specifiers that define how you want<br />

each data item to be interpreted. For example, specify %s for string data, %f<br />

for floating-point data, and so on. (For a complete list of format specifiers, see<br />

the textread reference page.)<br />

Foreachconversionspecifierinyour format string, you must specify a<br />

separate output variable. textread processeseachdataiteminthefileas<br />

specified in the format string and puts the value in the output variable. The<br />

6-81

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!