필터 지우기
필터 지우기

Input dialog boxes accepts binary bit sequence

조회 수: 3 (최근 30일)
Lorrenzo Francis
Lorrenzo Francis 2019년 3월 13일
댓글: Lorrenzo Francis 2019년 3월 14일
After entering a binary bit sequence in an input dialog box, I'm having issues when I try to convert it to a numeric value. When the str2num function is used, the leading zeros are erased. Below is my code:
prompt = {'Enter bit sequence (max 8 bits):'};
ititle = 'Input';
dims = [1 35];
temp1 = inputdlg(prompt,ititle,dims);
temp2 = str2num(temp1{1});
bit_sequence = str2num(num2str(temp2).');
disp(bit_sequence)
If the input is 0110, the output is
1
1
0
I would like to know how to keep the leading zero.

채택된 답변

Brian Hart
Brian Hart 2019년 3월 13일
Hi Lorrenzo,
MATLAB doesn't support binary representation. So when you run the above code, MATLAB thinks the value in temp2 is one-hundred-ten, not six.
To get the numerical value, try
>> temp2 = bin2dec(temp1{1});
Then to display the value as binary, try
>> disp(dec2bin(temp2,4))
where the "4" tells MATLAB how many bits to display.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by