23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6 Data Import and Export<br />

The act of reading advances the file position indicator. To determine the<br />

current file position indicator, call ftell:<br />

position = ftell(fid)<br />

position =<br />

8<br />

This call to fseek moves the file position indicator back 4 bytes:<br />

status = fseek(fid,-4,'cof');<br />

Calling fread again reads in the next value (3):<br />

three = fread(fid,1,'short');<br />

ReadingStringsLinebyLinefromTextFiles<br />

<strong>MATLAB</strong> provides two functions, fgetl and fgets, that read lines from<br />

formatted text files and store them in string vectors. The two functions are<br />

almost identical; the only difference is that fgets copies the newline character<br />

to the string vector but fgetl does not.<br />

The following M-file function demonstrates a possible use of fgetl. This<br />

function uses fgetl to read an entire file one line at a time. For each line,<br />

the function determines whether an input literal string (literal) appearsin<br />

the line.<br />

If it does, the function prints the entire line preceded by the number of times<br />

the literal string appears on the line.<br />

6-110

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

Saved successfully!

Ooh no, something went wrong!