MATLAB: An efficient (quick) way of entering complex data into Matlab workspace.

complexfreadMATLABmemmapfilemex compiler

A HW device stores binary data on disk as an array of 32.5e6 unsigned short values which are then read into Matlab workspace, using either fread or memmapfile. The data is actually complex in nature, arranged as I/Q/I/Q/,,, Our goal is to convert the short values into a complex array so that we may perform FFT and other Matlab operations. The problem is that the conversions to single and then to complex are each very time consuming; please see below.
datasize = 32.5e6;
fileID = fopen('dataFile_uint16.bin', 'r');
temp = fread(fileID, dataSize, 'int16=>int16'); % read file as I & Q; memmapfile is faster...
fclose(fileID);
temp1 = single(temp); % Takes between 100-200 ms.
% NOTE: Matlab does not allow short
% data to be converted to complex
complexArray = temp1(1:2:dataSize) + 1i*temp1(2:2:dataSize); % takes ~550 ms
I have tried alternatives to the last line of code above such as complex(a,b) and reshape instead of indexing, but no real-time savings is noticed. Any suggestions?
A rigourous alternative is to write a mex-file which reads, converts, and returns the data to an mxComplexArray type. However, before enbarking on such a journey, I would like to know if the savings in real-time will justify the effort.
Thank you,
Yisrael Loecher

Best Answer

Assuming you are using R2018a or later, you might try this FEX submission which can reinterpret real variables as interleaved complex variables without a deep copy:
This might save you some time in your second step, but of course won't save any time associated with that first step of converting the uint16 to single. But if the original data is stored as uint16 you are probably just going to have to live with the time it takes to convert it all to single.