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.

6 Data Import and Export<br />

A = dlmread('ph.dat', ';');<br />

You specify the delimiter used in the data file as the second argument to<br />

dlmread. Note that, even though the last items in each row are not followed<br />

by a delimiter, dlmread can still process the file correctly. dlmread ignores<br />

space characters between data elements. So, the preceding dlmread command<br />

works even if the contents of ph.dat are<br />

7.2; 8.5; 6.2;6.6<br />

5.4; 9.2 ;8.1;7.2<br />

Importing Numeric Data with Text Headers<br />

To import an ASCII data file that contains text headers, use the textscan<br />

function, specifying the headerlines parameter. textscan accepts a set of<br />

predefined parameters that control various aspects of the conversion. (For a<br />

complete list of these parameters, see the textscan reference page.) Using<br />

the headerlines parameter, you can specify the number of lines at the head<br />

of the file that textscan should ignore.<br />

For example, the file grades.dat contains formatted numeric data with a<br />

one-line text header:<br />

Grade1 Grade2 Grade3<br />

78.8 55.9 45.9<br />

99.5 66.8 78.0<br />

89.5 77.0 56.7<br />

To import this data, first open the file and then use this textscan command<br />

to read the contents:<br />

fid = fopen('grades.dat', 'r');<br />

grades = textscan(fid, '%f %f %f', 3, 'headerlines', 1);<br />

grades{:}<br />

ans =<br />

78.8000<br />

99.5000<br />

89.5000<br />

ans =<br />

55.9000<br />

6-80

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

Saved successfully!

Ooh no, something went wrong!