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

• “Example 4 — Modifying Map Parameters” on page 6-58<br />

Improving Performance<br />

<strong>MATLAB</strong> accesses data in structures more efficiently than it does data<br />

contained in objects. The main reason is that structures do not require the<br />

extra overhead of a subsref routine. Instead of reading directly from the<br />

memmapfile object, as shown here<br />

for k = 1 : N<br />

y(k) = m.Data(k);<br />

end<br />

you will get better performance when you assign the Data field to a variable<br />

and then read or write the mapped file through this variable, as shown in<br />

this second example:<br />

dataRef = m.Data;<br />

for k = 1 : N<br />

y(k) = dataRef(k);<br />

end<br />

Example 1 — Reading a Single Data Type<br />

This example maps a file of 100 double-precision floating-point numbers to<br />

memory. The map begins 1024 bytes from the start of the file, and ends 800<br />

bytes (8 bytes per double times a Repeat value of 100) from that point.<br />

If you haven’t done so already, generate a test data file for use in the following<br />

examples by executing the gendatafile function defined under “Constructing<br />

a memmapfile Object” on page 6-40:<br />

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

Now, construct the memmapfile object m, and show the format of its Data<br />

property:<br />

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

'Offset', 1024, 'Repeat', 100);<br />

6-55

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

Saved successfully!

Ooh no, something went wrong!