%% Read from 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 Map 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

% 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)

%
%% Read from Memory Map 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 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)

%
% Read the x field of that structure from the file.

d(3).x

% 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

% 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)

% Copyright 2015 The MathWorks, Inc.