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.

Using Low-Level File I/O Functions<br />

fid is the file identifier for the file. offset is a positive or negative offset<br />

value, specified in bytes. origin is one of the following strings that specify<br />

the location in the file from which to calculate the position.<br />

'bof'<br />

'cof'<br />

'eof'<br />

Beginning of file<br />

Current position in file<br />

End of file<br />

Understanding File Position<br />

To see how fseek and ftell work, consider this short M-file:<br />

A = 1:5;<br />

fid = fopen('five.bin','w');<br />

fwrite(fid, A,'short');<br />

status = fclose(fid);<br />

This code writes out the numbers 1 through 5 to a binary file named five.bin.<br />

The call to fwrite specifies that each numerical element be stored as a short.<br />

Consequently, each number uses two storage bytes.<br />

Now reopen five.bin for reading:<br />

fid = fopen('five.bin','r');<br />

This call to fseek moves the file position indicator forward 6 bytes from the<br />

beginning of the file:<br />

status = fseek(fid,6,'bof');<br />

This call to fread reads whatever is at file positions 7 and 8 and stores it<br />

in variable four:<br />

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

6-109

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

Saved successfully!

Ooh no, something went wrong!