필터 지우기
필터 지우기

How to convert a .txt file of 16 bit binary values into signed 16bit integers

조회 수: 17 (최근 30일)
I have a .txt file of 16 bit binary numbers. However, the numbers are stored as bytes seperated by spaces, ex. 00001111 00001111 11110000 11110000 are the decimal numbers 3855 and 61680. I want Matlab to read the text file, removing every other space to form seamless 16bit binary numbers. I then want these 16bit binary numbers to be converted into signed 16 bit integer using signed 2's compliment.
I know that I can use fopen and fileread to read the numbers from the file, but I don't know how to remove every other space to form seamless 16bit binary numbers. I am also not sure how to properly convert from 16bit binary (signed 2's compliment) to signed 16 bit integers.
I have included an example .txt file. The decimal representations of the numbers are 2, 64, 128, and 2048.
Any help regarding what functions/algorithms to use is greatly appreciated.

채택된 답변

Simon Chan
Simon Chan 2021년 7월 8일
A=readcell('Binary.txt');
B = strsplit(A{:},' ');
C=reshape(B,2,[])';
D=cellfun(@(x,y) horzcat(x,y),C(:,1), C(:,2), 'UniformOutput', false);
E=int16(bin2dec(D));
E =
4×1 int16 column vector
2
64
128
2048
  댓글 수: 3
Simon Chan
Simon Chan 2021년 7월 8일
Sorry, overlook signed 2's compliment
E = bin2dec(D);
F = int16(E-(E>32768)*65536)
Samuel Wiest
Samuel Wiest 2021년 7월 8일
Now it works perfectly, thank you so much!

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

추가 답변 (1개)

Kapil Gupta
Kapil Gupta 2021년 7월 8일
I assume you want to know how you can deal with 16 bit values. The following MATLAB Answers link has a similar query, you can check this out:

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by