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

댓글 수: 1

Stephen23
Stephen23 2017년 9월 20일
Read the binary data as characters, not as numeric.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2017년 9월 20일

0 개 추천

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

Rik
Rik 2017년 9월 20일
And of course you can follow this up with functions like bin2dec.
Karl Haebler
Karl Haebler 2017년 9월 20일
Thanks for the response. When I do use a string, it joins all of the binary numbers on different rows onto one huge string of numbers. So the results is a string of numbers thousands of digits long, instead of a vector with many strings as the entries. I'll see if I can fix that but if you have a suggestion that'd be appreciated. Thanks.
Jan
Jan 2017년 9월 20일
편집: Jan 2017년 9월 20일
How does your file look like? What about:
FileCont = strsplit(filerad(FileName), '\n');
Data = zeros(size(FileCont));
for iData = 1:numel(Data)
Data(iData) = bin2dec(FileCont{iData}); % [EDITED, Data->FileCont]
end
Karl Haebler
Karl Haebler 2017년 9월 20일
Hey Jan, I've attached my exact text file. I need to make a vector with each 12 bit binary number a string in this vector. That's the first step, next step is to convert this signed (2s complement) binary to decimal but I'll tackle that later. For now I just want a vector with all of this binary data as a bunch of strings. If you need more clarification please let me know. Thanks for the help!
Guillaume
Guillaume 2017년 9월 20일
Have you actually tried the solutions given to you (Jan's or mine).
They both read the whole file at once then convert it into a cell vector of char array using strsplit (Note that, as in my answer, you don't need to specify '\n' as it's the default).
I think Jan got a little mixed-up afterward in his loop, it's probably meant to be bin2dec(FileCont{iData}), but both do the same use bin2dec to convert the char arrays to decimal. As per my answer, there's no need for a loop, bin2dec is perfectly happy with the cell array and even if it wasn't, the cell array can be converted to an Nx12 char array using char, so no looping required again.
Karl Haebler
Karl Haebler 2017년 9월 20일
Hey Guillaume, yes I have tried yours and Jan's suggestions. I couldn't get them to work (I guess cause I'm new and don't really know what's going on), but I did end up using a textscan which did get it to work. And the data is stored in cells, like you mention. However, the conversion to decimal is different than you suggest since this is signed binary (represents positive and negative decimal numbers), and bin2dec works with unsigned data. I can find a way to convert to signed decimal if the binary data is 8 or 16 bit, but here it is 12 bit. So I'm stuck, now I don't know how to convert to 12 bit signed decimal. I'll try to figure it out but if you know I'd appreciate the help!
Karl Haebler
Karl Haebler 2017년 9월 20일
Actually I figured out the binary conversion with 12 bits!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2017년 9월 20일

2 개 추천

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
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.
Jan
Jan 2017년 9월 20일
+1: I've overseen the detail, that bin2dec works on cell strings directly.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2017년 9월 20일

편집:

2017년 9월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by