Programming Previous page   Next Page

Reading a Mapped File

The most commonly used property of the memmapfile class is the data property. It is through this property of the memory-map object that MATLAB provides all read and write access to the contents of the mapped file.

The actual mapping of a file to the MATLAB address space does not take place when you construct a memmapfile object. A memory map, based on the information currently stored in the mapped object, is generated the first time you reference or modify the data property for that object.

Once you have mapped a file to memory, you can read the contents of that file using the same MATLAB statements used to read variables from the MATLAB workspace. By accessing the data property of the memory map object, the contents of the mapped file appear as if they were an array in the currently active workspace. You simply index into this array to read the desired data from the file.

Example 1 -- Reading a Single Data Type

This example maps a file of 100 single-precision floating-point numbers into memory. The map begins 1024 bytes from the start of the file, and ends 400 bytes (4 bytes per single times a repeat value of 100) from that point. Construct the memmapfile object m, and show the format of its data property:

Read a selected set of numbers from the file by indexing into the single-precision array m.data:

Example 2 -- Formatting File Data as a Matrix

This example is similar to the last, except that the constructor of the memmapfile object now specifies an array shape of 4-by-6 to be applied to the data as it is read from the mapped file. MATLAB maps the file contents into a structure array rather than a numeric array, as in the previous example:

When you read an element of the structure array, MATLAB presents the data in the form of a 4-by-6 array:

To index into the structure array field, use

Example 3 -- Reading Multiple Data Types

This example maps a file containing more than one data type. The different data types contained in the file are mapped as fields of the returned structure array m.data.

The format parameter passed in the constructor specifies that the first 80 bytes of the file are to be treated as a 5-by-8 matrix of uint16, and the 160 bytes after that as a 4-by-5 matrix of double. This pattern repeats until the end of the file is reached. The example shows different ways of reading the data property of the object.

Start by calling the memmapfile constructor to create a memory map object, m:

If you examine the data property, MATLAB shows an 83-element structure array with two fields, one for each format specifier in the constructor:

Examine one structure in the array to show the format of each field:

Now read the x and y fields of that structure from the file. MATLAB formats the first block of data as a 5-by-8 matrix of uint16, as specified in the format property, and the second block as a 4-by-5 matrix of double:

To work with multiple blocks of the mapped file, assign the full structure array m.data to a variable, s, and index into the array using standard MATLAB indexing. The example reads three regions of type double from the file:

Example 4 -- Modifying Map Parameters

This example plots the Fourier transform output of data read from a file via a memory map. It then modifies several parameters of the existing map, reads from a different part of the data file, and plots a histogram from that data.

Create a memory-mapped object, mapping 10,000 elements of type double starting at the 1025th byte:

Get data associated with the map and plot the FFT of the first 1024 values of the map. This is when the map is actually created, because no data has been referenced until this point:

Get information about the memory map:

Change the map, but continue using the same file:

Read from a different area of the file, and plot a histogram of the data. This maps a new region and unmaps the previous region:


Previous page  Constructing a memmapfile Object Writing to a Mapped File Next page

© 1994-2005 The MathWorks, Inc.