I have a .bin file (having only 0's and 1's). And I want to consider 2 bits of the data from the file at a time in an iterative manner till all of the digits are read.

2 views (last 30 days)
How Do I do it ?
Question Explanation :
Suppose I have text written in binary format as: 0010011110100111
I want to access two bits of data at a time. For example, first I need to access "00" and I'll perform another algortihm then I want to access "10" and so on till all the digits are read.

Answers (1)

Walter Roberson
Walter Roberson on 1 Aug 2022
twobits = fread(fid, [1 2], 'uchar');
This will not be efficient. Unless you have reason to expect that you might run out of memory, read the entire file at the same time, reshape to two rows, and index the columns
  3 Comments
Walter Roberson
Walter Roberson on 1 Aug 2022
'uchar' always converts exactly one byte per entry. 'char' on the other hand scans the file the first time to try to figure out what encoding it uses, then rewinds and potentially reads multiple bytes per entry, if matlab guessed utf8 or similar and the current byte looks like it might be part of a multi byte sequence.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!