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.

Accessing Files with Memory-Mapping<br />

ans =<br />

500 500 500 500 500 500 500 500<br />

500 500 500 500 500 500 500 500<br />

500 500 500 500 500 500 500 500<br />

Selecting Appropriate Data Types<br />

All of the usual <strong>MATLAB</strong> indexing and data type rules apply when assigning<br />

values to data via a memory map. The data type that you assign to must be<br />

big enough to hold the value being assigned. For example,<br />

m = memmapfile('records.dat', 'Format', 'uint8', ...<br />

'Writable', true);<br />

d = m.Data;<br />

d(5) = 300;<br />

saturates the x variable because x is defined as an 8-bit integer:<br />

d(5)<br />

ans =<br />

255<br />

Working with Copies of the Mapped Data<br />

In the following code, the data in variable block2 is a copy ofthefiledata<br />

mapped by m.Data(2). Because it is a copy, modifying array data in block2<br />

does not modify the data contained in the file:<br />

First, destroy the memmapfile object and restore the test file records.dat,<br />

since it has been modified by running the previous examples:<br />

clear m<br />

gendatafile('records.dat',50000);<br />

Map the file as a series of uint16 and double matrices and make a copy of<br />

m.Data(2) in block2:<br />

m = memmapfile('records.dat', ...<br />

'Format', { ...<br />

'uint16' [5 8] 'x'; ...<br />

'double' [4 5] 'y' });<br />

6-65

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

Saved successfully!

Ooh no, something went wrong!