Documentation Center

  • Trial Software
  • Product Updates

Read Mapped File

This example shows how to create two different memory maps, and then read from each of the maps using the appropriate syntax. Then, it shows how to modify map properties and analyze your data.

You can read the contents of a file that you mapped to memory using the same MATLAB® commands you use to read variables from the MATLAB workspace. By accessing the Data property of the memory map, the contents of the mapped file appear as an array in the currently active workspace. To read the data you want from the file, simply index into the array. For better performance, copy the Data field to a variable, and then read the mapped file using this variable:

dataRef = m.Data;
for k = 1 : N
   y(k) = dataRef(k);
end

By contrast, reading directly from the memmapfile object is slower:

for k = 1 : N
   y(k) = m.Data(k);
end

Read from Memory Mapped as Numeric Array

First, create a sample data file named records.dat that contains a 5000-by-1 matrix of double-precision floating-point numbers.

randData = gallery('uniformdata',[5000,1],0);

fileID = fopen('records.dat','w');
fwrite(fileID,randData,'double');
fclose(fileID);

Map 100 double-precision floating-point numbers from the file to memory, and then read a portion of the mapped data. Create the memory map, m. Specify an Offset value of 1024 to begin the map 1024 bytes from the start of the file. Specify a Repeat value of 100 to map 100 values.

m = memmapfile('records.dat','Format','double', ...
      'Offset',1024,'Repeat',100);

Copy the Data property to a variable, d. Then, show the format of d.

d = m.Data;

whos d
  Name        Size            Bytes  Class     Attributes

  d         100x1               800  double              

The mapped data is an 800-byte array because there are 100 double values, each requiring 8 bytes.

Read a selected set of numbers from the file by indexing into the vector, d.

d(15:20)
ans =

    0.8392
    0.6288
    0.1338
    0.2071
    0.6072
    0.6299

Read from Memory Mapped as Nonscalar Structure

Map portions of data in the file, records.dat, as a sequence of multiple data types.

Call the memmapfile function to create a memory map, m.

  m = memmapfile('records.dat',  ...
      'Format', {              ...
         'uint16' [5 8] 'x';   ...
         'double' [4 5] 'y' });

The Format parameter tells memmapfile to treat the first 80 bytes of the file as a 5-by-8 matrix of uint16 values, and the 160 bytes after that as a 4-by-5 matrix of double values. This pattern repeats until the end of the file is reached.

Copy the Data property to a variable, d.

d = m.Data
d = 

166x1 struct array with fields:

    x
    y

d is a 166-element structure array with two fields. d is a nonscalar structure array because the file is mapped as a repeating sequence of multiple data types.

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

d(3)
ans = 

    x: [5x8 uint16]
    y: [4x5 double]

Read the x field of that structure from the file.

d(3).x
ans =

  19972  47529  19145  16356  46507  47978  35550  16341
  60686  51944  16362  58647  35418  58072  16338  62509
  51075  16364  54226  34395   8341  16341  33787  57669
  16351  35598   6686  11480  16357  28709  36239   5932
  44292  15577  41755  16362  30311  31712  54813  16353

MATLAB formats the block of data as a 5-by-8 matrix of uint16 values, as specified by the Format property.

Read the y field of that structure from the file.

d(3).y
ans =

    0.7271    0.3704    0.6946    0.5226    0.2714
    0.3093    0.7027    0.6213    0.8801    0.2523
    0.8385    0.5466    0.7948    0.1730    0.8757
    0.5681    0.4449    0.9568    0.9797    0.7373

MATLAB formats the block of data as a 4-by-5 matrix of double values.

Modify Map Properties and Analyze Data

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

Create a sample file named double.dat.

randData = gallery('uniformdata',[5000,1],0);
fileID = fopen('double.dat','w');
fwrite(fileID,randData,'double');
fclose(fileID);

Create a memmapfile object of 1,000 elements of type double, starting at the 1025th byte.

m = memmapfile('double.dat','Offset',1024,  ...
      'Format','double','Repeat',1000);

Copy the Data property to a variable, k. Then, get data associated with the map and plot the FFT of the first 100 values of the map.

k = m.Data;
plot(abs(fft(k(1:100))))

This is the first time that data is referenced and is when the actual mapping of the file to the MATLAB address space takes place.

Change the map properties, but continue using the same file. Whenever you change the value of a memory map property, MATLAB remaps the file to memory.

m.Offset = 4096;
m.Format = 'single';
m.Repeat = 800;

m is now a memmapfile object of 800 elements of type single. The map now begins at the 4096th byte in the file, records.dat.

Read from the portion of the file that begins at the 4096th byte, and calculate the maximum value of the data. This command maps a new region and unmaps the previous region.

X = max(m.Data)
X =

  7.5449e+37

See Also

More About

Was this topic helpful?