0

I have a very huge 3D matrix, the data was written into disk for future use. Writing the matrix into a bin is easy, reading it back however have some issue.

Write to bin:

z=repmat(complex(rand(5),rand(5)),[1 1 5])
z_imag = imag(z);
z_real = real(z);
adjacent = [z_real z_imag];
fileID = fopen('complex.bin','w');
fwrite(fileID,adjacent,'double')

And now, I try to read it back using memmapfile:

m = memmapfile('complex.bin', 'Offset', 0, 'Format', {'double' [5,5,5] 'x'});
complexValues = complex(m.Data(:).x(1,:), m.Data(:).x(2,:)); %this line doesn't work though, just for explanation's sake

It gave me an error saying that

Error using memmapfile/subsref (line 764) A subscripting operation on the Data field attempted to create a comma-separated list. The memmapfile class does not support the use of comma-separated lists when subscripting.

I was referring to the solution here, the suggested solution used the reshape to shape the matrix beforehand (as contrast to my method above). I try to avoid using reshape in my code as I'm dealing with very huge data and that might computationally expensive and takes a long time. Is there an alternative/better way to do this?

Thanks in advance!

Gregor Isack
  • 1,111
  • 12
  • 25
  • `reshape` doesn’t copy data, it’s cheap. In contrast, your method of concatenating matrices does, it’s expensive. – Cris Luengo Dec 02 '18 at 20:29
  • What version of MATLAB are you using? R2018a and above store complex values together, “interleaved”, rather than in separate blocks of memory as it did before. The solution will be very different depending on the version. – Cris Luengo Dec 02 '18 at 20:30
  • Oh I didn't know that, and actually I'm using version R2018b, so I guess I could skip the concatenation part before store the matrix? But back to the question, how do I read it back though? Because at the moment, the valued mapped to the memory is all real numbers, not a complex number – Gregor Isack Dec 03 '18 at 03:38
  • @CrisLuengo btw when you said for Matlab version R2018a and above stored complex values together, I tried and it doesn't seems so, it only stored the real value. And the binary file is supposedly twice the size if it stored both complex values together no? It's not the case though. (I can't seems to find any Matlab documentation mentioned about that, except [this](https://www.mathworks.com/help/matlab/import_export/exporting-binary-data-with-low-level-i-o.html#br48alm)) – Gregor Isack Dec 03 '18 at 05:00
  • No, I was referring to internal storage. The way a complex matrix is stored in memory has changed, and is now interleaved. See [this question](https://stackoverflow.com/questions/52635035/casting-complex-to-real-without-data-copy-in-matlab-r2018a-and-newer), the accepted answer there would be part of your solution, I think. – Cris Luengo Dec 03 '18 at 05:21

0 Answers0