As requested, I show an example memory mapped files using memmapfile
.
Since you did not specify the exact data file format, I will create my own. The data I create is a table of N
rows, each of which consists of 4 columns:
- is the scalar value of
double
- second is a
single
value - third is a fixed-length string representing
uint32
in HEX notation (for example: D091BB44
) - The fourth column is the
uint8
value
Code to generate random data and write it to a binary file, structured as described above:
% random data N = 10; data = [... num2cell(rand(N,1)), ... num2cell(rand(N,1,'single')), ... cellstr(dec2hex(randi(intmax('uint32'), [N,1]),8)), ... num2cell(randi([0 255], [N,1], 'uint8')) ... ]; % write to binary file fid = fopen('file.bin', 'wb'); for i=1:N fwrite(fid, data{i,1}, 'double'); fwrite(fid, data{i,2}, 'single'); fwrite(fid, data{i,3}, 'char'); fwrite(fid, data{i,4}, 'uint8'); end fclose(fid);
Here is the resulting file viewed in the HEX editor:

we can confirm the first entry (note that my system uses the byte order of bytes):
>> num2hex(data{1,1}) ans = 3fd4d780d56f2ca6 >> num2hex(data{1,2}) ans = 3ddd473e >> arrayfun(@dec2hex, double(data{1,3}), 'UniformOutput',false) ans = '46' '35' '36' '32' '37' '35' '32' '46' >> dec2hex(data{1,4}) ans = C0
Next, we open the file using memory mapping:
m = memmapfile('file.bin', 'Offset',0, 'Repeat',Inf, 'Writable',false, ... 'Format',{ 'double', [1 1], 'd'; 'single', [1 1], 's'; 'uint8' , [1 8], 'h'; % since it doesnt directly support char 'uint8' , [1 1], 'i'});
Now we can access the records as a regular structural array:
>> rec = m.Data; % 10x1 struct array >> rec(1) % same as: data(1,:) ans = d: 0.3257 s: 0.1080 h: [70 53 54 50 55 53 50 70] i: 192 >> rec(4).d % same as: data{4,1} ans = 0.5799 >> char(rec(10).h) % same as: data{10,3} ans = 2B2F493F
The advantage is that for large data files you can limit the display of the βviewportβ to a small subset of records and move this view along the file:
% read the records two at-a-time numRec = 10; % total number of records lenRec = 8*1 + 4*1 + 1*8 + 1*1; % length of each record in bytes numRecPerView = 2; % how many records in a viewing window m.Repeat = numRecPerView; for i=1:(numRec/numRecPerView) % move the window along the file m.Offset = (i-1) * numRecPerView*lenRec; % read the two records in this window: %for j=1:numRecPerView, m.Data(j), end m.Data(1) m.Data(2) end
