Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Binary format reading into matlab

1 vue (au cours des 30 derniers jours)
Raviteja
Raviteja le 7 Avr 2011
Clôturé : MATLAB Answer Bot le 20 Août 2021
Hello,
clear all
clc
Input_Binary=sprintf('Enter binary data \nHit Enter after completetion')
i=1;
while 1
d=input('');
if d==0 || d==1
x(i)=logical(d);
else
if d=='\n'
disp('input ended');
break;
else
disp('wrong binary input: continue input');
continue
end
end
d=0;
i=i+1;
end
Aim of the program:
I want to take 10110101010100101010 as single input. But I have to store it in variable x in logical format
>>x= 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0
For this I have written the above code.
Following Observations: 1. The matlab function input() is taking any length of digit. But I need only one input value either 0 or 1. How to do it?
2. When I hit enter, disp('input ended'); is not working. How to do this
3. The following error is showing when I do hit enter twice
??? Operands to the || and && operators must be convertible to logical
scalar values.
Error in ==> readbianary at 12
if d==0 || d==1
Why?

Réponses (2)

Walter Roberson
Walter Roberson le 7 Avr 2011
When the user presses return without entering any data, the empty array is returned.
If you require that a single keystroke be read at a time, then you might be able to use one of the contributions to the MATLAB File Exchange.
  1 commentaire
Raviteja
Raviteja le 7 Avr 2011
Can you explain me the 3 issue of my question?

Fangjun Jiang
Fangjun Jiang le 7 Avr 2011
Use one input() to get a string of '01010100100', then do the following:
A='0101010101010110';
B=str2num(A(:))

Community Treasure Hunt

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

Start Hunting!

Translated by