memmapfile and alternative data formats (char, complex, etc.)

9 views (last 30 days)
This is both a request for expanded Matlab capabilities in future versions, and a question as to whether better methods than used below exist. I am attempting to use 'memmapfile' to read large binary files that have a custom header. Most of the data formats are standard (uint32,double, etc.) but there are a few that were written in formats not currently supported by memmapfile. E.g. as 'char' by a client using python. The payload itself is a complex64 (complex single) which is readable from Python.
(1) How can I read 'char' properly? Right now I have them just reading in as uint8's to get the correct size, so I either need to reinterpret them as 'char' (how?) or to read them as chars directly.
(2) How can I read a complex? Right now, the assumption (correct for this set) that each complex64 is really a pair of singles in the order [real, imag]. Thus, I can recreate the complex single using something of the form
dat = m.Data(1:2:end) + 1i*m.Data(2:2:end)
where m is my memory map. This works...but is so cludgy feeling that I wonder if there is a better way.
Regardless, expanding the supported formats for memmapfile would be appreciated.
Thanks, -Dan

Accepted Answer

Vandana Ravichandran
Vandana Ravichandran on 14 Apr 2017
"memmapfile" currently supports int/uint (different variants), single, double data types. You can use char and complex with "memapfile" in the following way -
1. "char"
% Create Data
myData = 'This is a string';
% Write char data to file
fileID = fopen('records.dat','w');
fwrite(fileID, myData,'char');
% memmapfile read data
>> m = memmapfile('records.dat');
>> data = char(m.Data)';
2. "complex"
% Create Data
complexData = complex([1:10],[20:-1:11]);
realData = real(complexData);
imaginaryData = imag(complexData);
interleavedData = reshape([realData;imaginaryData],[],1);
% Write complex data to file
fid = fopen('complexData.dat','w');
fwrite(fid, interleavedData, 'double');
% MEMMAPFILE - formats data into 2 rows of 10 elements each
m = memmapfile('complexData.dat', 'Offset', 0, 'Format', {'double' [2,10] 'x'});
complexValues = complex(m.Data(:).x(1,:), m.Data(:).x(2,:));

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!