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 />

Controlling the Number of Values Read<br />

fread accepts an optional second argument that controls the number of<br />

values read (if unspecified, the default is the entire file). For example, this<br />

statement reads the first 100 data values of the file specified by fid into<br />

the column vector A.<br />

A = fread(fid,100);<br />

Replacing the number 100 with the matrix dimensions [10 10] reads the<br />

same 100 elements into a 10-by-10 array.<br />

Controlling the Data Type of Each Value<br />

An optional third argument to fread controls the data type of the input. The<br />

data type argument controls both the number of bits read for each value and<br />

the interpretation of those bits as character, integer, or floating-point values.<br />

<strong>MATLAB</strong> supports a wide range of precisions, which you can specify with<br />

<strong>MATLAB</strong> specific strings or their C or Fortran equivalents.<br />

Some common precisions include<br />

• 'char' and 'uchar' for signed and unsigned characters (usually 8 bits)<br />

• 'short' and 'long' for short and long integers (usually 16 and 32 bits,<br />

respectively)<br />

• 'float' and 'double' for single- and double-precision floating-point<br />

values (usually 32 and 64 bits, respectively)<br />

Note The meaning of a given precision can vary across different hardware<br />

platforms. For example, a 'uchar' is not always 8 bits. fread also provides<br />

a number of more specific precisions, such as 'int8' and 'float32'. If<br />

in doubt, use precisions that are not platform dependent. See fread for<br />

a complete list of precisions.<br />

For example, if fid refers to an open file containing single-precision<br />

floating-point values, then the following command reads the next 10<br />

floating-point values into a column vector A:<br />

A = fread(fid,10,'float');<br />

6-107

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

Saved successfully!

Ooh no, something went wrong!