Is there a limit to the number of elements in an array in Matlab
조회 수: 19 (최근 30일)
이전 댓글 표시
I have a binary file that comprises 16,814,160 bytes of 16-bit data in little endian format. I wish to open the file in Matlab and convert the 16-bit values to decimal in a 1 x 8407080 array. The first 8 bytes of the file in hex are: E0 0B 23 0C AD 0B 23 0C.
When I edit the file so that it comprises only the first 8 bytes, and then run the commands below, it returns [3040,3107,2989,3107] in the Workspace. This is as I would expect.
However, when I run the same program with all 16,814,160 bytes, the output is shown in the Workspace as a 1x4147200 double, of which the first four numbers are [56802,57570,56800,56801 and so on]. I would have expected the first four bytes to have remained unchanged!
Why is the array truncated to 4147200 elements and why have the decimal values been changed? The only difference is the input file size.
filename='my_binary_file(first_8_bytes).raw';
%filename='my_binary_file(all_bytes).raw' %uncomment this when reading in file size 16,814,160 bytes
fileID = fopen(filename, 'r', 'ieee-le'); % open the file for low level I/O function and obtain file identifier
if fileID == -1, error('Cannot open file: %s', filename); end
precision = 'uint16';
Data = fread(fileID, [1 8407080], precision); %reads data at byte level
fclose(fileID);
Thanks for your help.
댓글 수: 0
채택된 답변
Jan
2021년 1월 26일
Check again, if you are really reading the file your expect you do. Use absolute path names to control this.
If the file starts with the bytes HEX(E0 0B 23 0C AD 0B 23 0C), importing it as UINT16 will reply [3040, 3107, 2989, 3107] in every case. If you get [56802,57570,56800,56801] and an unexpected number of elements, your are reading another file. Everything else would be pure magic, but Matlab works deterministically.
추가 답변 (2개)
Steven Lord
2021년 1월 26일
If you want the data to be read in as uint16 and then returned as uint16 as well, use '*uint16' as the precision input in your call to fread. The asterisk is very important. See the description of the precision input on the documentation page for the fread function for more information about the difference.
Walter Roberson
2021년 1월 26일
The limit to the number of elements in an array in MATLAB is 2^48-1 which is roughly 2.8e14.
This is a design limit and Mathworks would need a Good Reason to increase it.
The limit was chosen because the Intel x64 architecture is designed with only 48 address bits. There are no publicly known implementations of x64 architecture that have more address lines.
The only publicly known implementation that approaches having that much memory is a server from HP, but you would need several of them tied together with unified address space in order to reach the limit. More of them than exist in the world, I believe.
댓글 수: 3
Walter Roberson
2021년 1월 26일
160 TB would be 1.6E14 bytes, so that is about 2/3 of the upper limit. I guess two of them together would exceed the limit.
참고 항목
카테고리
Help Center 및 File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!