what this functions exactly do (fwrite) and (ubitN)?
20 views (last 30 days)
John D'Errico on 9 Jan 2022
ubitN is not part of MATLAB, not is it in any MATLAB toolbox. You need to contact the source.
Ok, someone will surely tell me that ubitN is ubitiquous. ;) (Spelling intentional there.)
More Answers (2)
Walter Roberson on 10 Jan 2022
When you use fread with precision 'ubit' followed by a number, such as 'ubit2' or 'ubit18', then fread reads the next number of bits specified by the number, and assigns each group of bits as an unsigned integer.
When you are reading more than one value in a single call, such as fread(f_id, [1 10], 'ubit2') then fread treats the input as a stream of bits. In the case of this example, the first 2 bits would go to the first output element, the next 2 bits would go to the second output element, and so on.
However, each time that you do a new fread(), you are alway starting at the beginning of a byte, and at the end of the fread() with a 'ubit*' specification, if there are any unused bits in the last byte that has been read in, then those bits are discarded. You cannot for example use do 'ubit3', 'ubit4', 'ubit9' in order to split the first 16 bits as 3 bits, then 4 bits, then 9 bits: when you end the 'ubit3' call, the 5 remaining bits in the first byte are discarded; then when you end the 'ubit4' call, the 4 remaining bits in the current (second in the file) byte are discarded, then the 8 bits from the next byte and the 1st bit from the byte after that would be read in, and the 7 bits at the end would be discarded.. for a total of 4 bytes read, with a number of the internal bits being discarded. But fread(f_id, [1 4], 'ubit4') would be happy to read 4 bits, 4 bits, 4 bits, 4 bits, for a total of 16 bits with no bits discarded -- the discarding is at the end of each call to fread()
yanqi liu on 10 Jan 2022
Edited: Walter Roberson on 10 Jan 2022