How to read 21-bit binary data stored as text?
Show older comments
I have a binary sequence stored as a text file. (See text file attached.) I have tried reading the file using csvread and fscanf.
Every time it wants to fill a column of data with scientific notation, then something gets lost in the translation back to binary then to decimal.
I keep getting twos and sometimes nines in my binary string and the bin2dec is not happy with twos and nines. It only knows how to read zeroes and ones.
I tried reading as %d and %ld, but the numbers I get are not even close to matching the binary values. I also tried to read as a string, but that reads in a continuous stream of characters with no break. I think it's looking for a space to break on. I have no spaces in the data, but there are likely carriage returns or linefeeds or both at the end of each line. Can fscanf be made to recognize carriage returns or linefeeds?
csvread example
%read 21-bit data using csvread
filename = '21bitsequence.csv';
raw_data = csvread(filename);
temp1 = num2str(raw_data,'%.0f');
temp2 = bin2dec(temp1);
fscanf example:
%read 21-bit data using fscanf
filename = '21bitsequence.csv';
fileID = fopen(filename, 'r');
formatSpec = '%f';
raw_data = fscanf(fileID, formatSpec);
temp1 = num2str(raw_data, '%.0f');
temp2 = bin2dec(temp1);
fclose(fileID);
Here's what I see in temp1:
val =
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110000000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110020000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110100000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111110120000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111000000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111020000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111100000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
111111111111111110000
Follow up question... What if I need to treat the binary as signed binary?
3 Comments
CAROL GARRITTY
on 25 May 2017
I'm sorry, but the signed part is not working properly. If I create a binary sequence from -128 to +128 (see attached) represented as signed binary (sign bit extended to all upper bits), the sequence does fine from -128 to -1 then it flips to +1048576 which is 2^20.
CAROL GARRITTY
on 25 May 2017
Edited: Walter Roberson
on 25 May 2017
Possible solution is to scale the data?
% read 21-bit signed binary data
temp = regexp(fileread('21bitsequence128.txt'), '\r?\n', 'split');
if isempty(temp{end}); temp(end) = []; end %empty after final \n
nums = bin2dec(temp);
signed_nums = nums;
num = length(signed_nums);
for i = 1:num
if (signed_nums(i) > 2^20-1);
signed_nums(i) = signed_nums(i) - 2^21;
end
end
Why doesn't this thing paste properly?
Walter Roberson
on 25 May 2017
Did you notice that I made a correction to the original code two days ago? "I just noticed and corrected a mistake in the positive case; please update any copy you have made."
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Conversion 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!