Storing data as binary strings
조회 수: 24 (최근 30일)
이전 댓글 표시
Hello, I am trying to read 12 bit binary data from several text file and store this information in a multidimensional array (CH1_Waveforms).
However, when I access one of these binary numbers (in CH1_Waveforms) they are represented as 1.0000e+11 instead of the full binary representation of 100001101110 or whichever binary number it is.
I am quite new to Matlab and don't know how to solve this problem, I suspect it has something to do with floating point notation. I have fiddled around with my code and can't find a solution.
How can I represent this data in it's full 12 bit binary representation and not an exponential which cuts off the data?
Any help would be greatly appreciated. Thanks
Here's my code now
numCH=2;
fileID=fopen('Matlab_Data.txt','r');
formatSpec='%f';
Mathematica=fscanf(fileID, formatSpec);
numseg=Mathematica(1);
domain=Mathematica(2);
i=1;
while i<=numseg
formatSpec='%f';
filename=sprintf('CH1_Seg_%d_Waveform.txt',i);
fileID=fopen(filename,'r');
CH1_Waveforms(i,:)=fscanf(fileID, formatSpec);
filename=sprintf('CH1_Trigger_Seg_%d_Waveform.txt',i);
fileID=fopen(filename,'r');
CH1_Waveforms(i,:,2)=fscanf(fileID, formatSpec);
i=i+1;
end
채택된 답변
Jan
2017년 9월 20일
Note:
'100001101110'
is a vector of characters, not a "binary number", although you can treat it in Matlab like it is one. Therefore you need to read this a string with the '%s' format specifier in fscanf.
댓글 수: 7
추가 답변 (1개)
Guillaume
2017년 9월 20일
binarystream = fileread(yourfile);
waveform = bin2dec(strsplit(binarystream));
should work and is very simple. That is assuming that the LSB in your text file is the right-most character. Otherwise, you'll have to reverse all the strings:
waveform = bin2dec(fliplr(char(strsplit(binarystream))));
댓글 수: 2
Guillaume
2017년 9월 20일
편집: Guillaume
2017년 9월 20일
"I couldn't get them to work"
Considering how simple the code is, it's puzzling why you couldn't get them to work. Showing us the exact code you actually used and the exact error you get (if any) would help to answer that question
I don't know how to convert to 12 bit signed decimal
Assuming a char array of Nx12 as produced by
binarydata = char(strsplit(fileread('CH1_Seg_1_Waveform.txt')))
then
signbit = binarydata(:, 1) - '0';
signeddecimal = bin2dec(binarydata) - signbit.*2^12
That's using 2's complement arithmetic with the sign bit as the MSB (1st character). It's fairly easy to cope with different endianness.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!