Why do I get "Out of memory." when reading only 16 chars?
조회 수: 1(최근 30일)
표시 이전 댓글
Dear Matlab community,
I want to read specific parts from a large (> 20 GB) binary file. However, the command
fails with "Out of memory." The command is applied very near to the beginning of the file (offset is 20 bytes).
Why do I get this error and how can I successfully read in my file?
Thank you for your suggestions,
댓글 수: 17
Sorry, I've been a bit too busy to follow answers recently but indeed I had conversations with Mathworks recently on text file parsing and indeed 2020a does automatic character set detection which is most likely the issue here. I'll post the details I've got from mathworks support in an answer.
"However what I could think of is that Matlab tries to guess the encoding"
I've had discussions with Mathworks support about this. The whole process is not properly documented unfortunately, which I told them can be a problem.
Indeed, if you open a file without specifying a character encoding, matlab will try to guess the file encoding the first time you either:
- use any character reading function such as fgetl, fgets, fscanf, etc.
- use fread with a 'char' or '*char' precision
- ask for the encoding with the multi-output version of fopen.
I haven't been given the full process of character set detection, but it does read the whole file which indeed can be an issue for large files. If any byte sequence in the file is not a valid UTF8 code point, then the algorithm uses some heuristics to see if it's a CJK encoding and if it still doesn't match, it assumes the local encoding.
To prevent this autodetection to take place, you have to specify an encoding when you fopen the file. If you don't know what the encoding is for your binary file, I'd suggest using 'US-ASCII'. As we mentioned in the comment, it's unlikely that a binary file uses UTF8 unless it prefixes the text by a length.
Unfortunately, it's not easy to go back to pre-2020a behaviour of automatically using the native encoding whatever it is, as R2020a has lost the ability of easily getting the local encoding. On the other hand, relying on native encoding when reading a binary file is asking for trouble.
댓글 수: 0
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!