How NOT remove zeros from binary number?

7 visualizzazioni (ultimi 30 giorni)
Hello,
i've a problem: when i have read binary data from device and when i have store this data in differents variables, MATLAB removed automaticly the first zero from every variable if this one starts with zero. If not starts with zero, all it's ok. I think that MATLAB made this for spare memory...
How could i made for matlab preserve this first zero of each variables?
For example:
Device gives me: 110101 and MATLAB stores 110101
but... if Device gives me: 010101 Matlab stores 10101
Remove the first zero.
Could you help me?
Thanks!
  2 Commenti
Image Analyst
Image Analyst il 12 Dic 2012
How are you capturing these numbers into MATLAB? What's the code? Are you capturing them as numbers? Can't you tell it to store the data into character strings?
Alberto Fernandez
Alberto Fernandez il 12 Dic 2012
Well..
first i've receive all this values in "dec" format. Then, i convert to "binary". And is in this step when i "loose" zero.
That's the code:
s = serial('COM5'); %crear objeto serial
set(s, 'BaudRate', 9600)
set(s, 'DataBits', 8)
set(s, 'InputBufferSize', 4096);
set(s, 'Parity','none')
set(s, 'StopBits', 1)
set(s, 'Terminator', 'LF/CR')
set(s, 'FlowControl', 'none')
%set(s, 'TimeOut', 5)
%POLL ALMANAC DATA%
fopen(s);
DataToSend = ['A0';'A2';'00';'02';'92';'00';'00';'92';'B0';'B3'];
DataDec = hex2dec(DataToSend);
fwrite(s,DataDec,'uint8');
DataReceive = fread(s);
DataHex = dec2hex(DataReceive);
fclose(s);
delete(s);
clear s
nel = numel( DataReceive);
valorsTrama = [];
x = 0;
i=1;
m=33;
j=1;
checksumTrama = [];
for i=m:nel
% var = DataReceive(i);
if DataReceive(i) == hex2dec(('A0'))
fprintf('Match 1')
if DataReceive(i+1) == hex2dec(('A2'))
fprintf('Match 2')
if DataReceive(i+2) == hex2dec(('00'))
fprintf('Match 3')
if DataReceive(i+3) == hex2dec(('1E'))
fprintf('Match 4')
if DataReceive(i+4) == hex2dec(('0E'))
fprintf('Match 5')
valorsTrama(:,j) = DataReceive(i + 4: i + 4 + 29);
checksumTrama(:,j) = DataReceive(i + 4 + 30 : i + 4 + 30+ 1);
i=i+m;
j=j+1;
end
end
end
end
end
end
for i=1:30
for j=1:32
valorsTramaHex{i,j} = dec2hex(valorsTrama(i,j));
end
end
Message_ID = [];
SV_ID = [];
for t=1:32
Message_ID = dec2hex(valorsTrama(1,:));
SV_ID = valorsTrama(2,:);
end
valorsTramaBin_1 = dec2bin(valorsTrama(3,:));
valorsTramaBin_2 = dec2bin(valorsTrama(4,:));
valorsTramaBin_3 = strcat(valorsTramaBin_1,valorsTramaBin_2);
valorsWeek = [];
for i=1:10
for j=1:32
valorsWeek = strcat(valorsTramaBin_1,valorsTramaBin_3(1,9),valorsTramaBin_3(1,10));
Week = bin2dec(valorsWeek);
end
end
Status = [];
for i=1:32
for j=1:7
valorsStatus = strcat('0',valorsTramaBin_2(1,2),valorsTramaBin_2(1,3),'0',valorsTramaBin_2(1,4),'0',valorsTramaBin_2(1,5),'0',valorsTramaBin_2(1,6),'0',valorsTramaBin_2(1,7));
Status{i,1} = bin2dec(valorsStatus);
end
end
valorsTramaBin_5 = dec2bin(valorsTrama(5,:));
valorsTramaBin_6 = dec2bin(valorsTrama(6,:));
valorsTramaBin_7 = dec2bin(valorsTrama(7,:));
valorsTramaBin_8 = dec2bin(valorsTrama(8,:));
valorsTramaBin_9 = dec2bin(valorsTrama(9,:));
valorsTramaBin_10 = dec2bin(valorsTrama(10,:));
valorsTramaBin_11 = dec2bin(valorsTrama(11,:));
valorsTramaBin_12 = dec2bin(valorsTrama(12,:));
valorsTramaBin_13 = dec2bin(valorsTrama(13,:));
valorsTramaBin_14 = dec2bin(valorsTrama(14,:));
valorsTramaBin_15 = dec2bin(valorsTrama(15,:));
valorsTramaBin_16 = dec2bin(valorsTrama(16,:));
valorsTramaBin_17 = dec2bin(valorsTrama(17,:));
valorsTramaBin_18 = dec2bin(valorsTrama(18,:));
valorsTramaBin_19 = dec2bin(valorsTrama(19,:));
valorsTramaBin_20 = dec2bin(valorsTrama(20,:));
valorsTramaBin_21 = dec2bin(valorsTrama(21,:));
valorsTramaBin_22 = dec2bin(valorsTrama(22,:));
valorsTramaBin_23 = dec2bin(valorsTrama(23,:));
valorsTramaBin_24 = dec2bin(valorsTrama(24,:));
valorsTramaBin_25 = dec2bin(valorsTrama(25,:));
valorsTramaBin_26 = dec2bin(valorsTrama(26,:));
valorsTramaBin_27 = dec2bin(valorsTrama(27,:));
valorsTramaBin_28 = dec2bin(valorsTrama(28,:));
Eccentricity = (bin2dec(strcat(valorsTramaBin_6, valorsTramaBin_7)))*2^(-21);
Time_of_Applicability = (bin2dec(strcat('0',valorsTramaBin_8)))*2^(12);
Orbital = (strcat(valorsTramaBin_9, valorsTramaBin_10));
val = [];
value = [];
Orbital_Inclination = [];
for i=1:32
val = bin2dec(Orbital(i,:));
y = sign(2^(16-1)-val)*(2^(16-1)-abs(2^(16-1)-val));
if ((y == 0) && (val ~= 0))
value{i} = -val;
else
value{i} = y;
end
end
dates = (cell2mat(value));
Orbital_Inclination = (dates*2^(-19)+0.30)*pi;
Rate_of_Right_Ascen = [];
RateAscen = (strcat(valorsTramaBin_11, '0',valorsTramaBin_12));
for i=1:32
val = bin2dec(RateAscen(i,:));
y = sign(2^(16-1)-val)*(2^(16-1)-abs(2^(16-1)-val));
if ((y == 0) && (val ~= 0))
value{i} = -val;
else
value{i} = y;
end
end
dates = (cell2mat(value));
Rate_of_Right_Ascen = (((dates*2^(-38)))*pi);
Raiz = bin2dec(strcat(valorsTramaBin_13,valorsTramaBin_14,'0000',valorsTramaBin_15,valorsTramaBin_16));
Raiz_A = Raiz*2^(-11);
Right_Ascen_at_Week = [];
RightAscenWeek = (strcat(valorsTramaBin_17,valorsTramaBin_18,valorsTramaBin_19));
for i=1:32
val = bin2dec(RightAscenWeek(i,:));
y = sign(2^(24-1)-val)*(2^(24-1)-abs(2^(24-1)-val));
if ((y == 0) && (val ~= 0))
value{i} = -val;
else
value{i} = y;
end
end
dates = (cell2mat(value));
Right_Ascen_at_Week = (((dates*2^(-23)))*pi);
Argument_of_Perigee = [];
omega = (strcat(valorsTramaBin_20,valorsTramaBin_21,valorsTramaBin_22));
for i=1:32
val = bin2dec(omega(i,:));
y = sign(2^(24-1)-val)*(2^(24-1)-abs(2^(24-1)-val));
if ((y == 0) && (val ~= 0))
value{i} = -val;
else
value{i} = y;
end
end
dates = (cell2mat(value));
Argument_of_Perigee = (((dates*2^(-23)))*pi);
Mean_Anom = [];
Mean = (strcat(valorsTramaBin_23,valorsTramaBin_24,valorsTramaBin_25));
for i=1:32
val = bin2dec(Mean(i,:));
y = sign(2^(24-1)-val)*(2^(24-1)-abs(2^(24-1)-val));
if ((y == 0) && (val ~= 0))
value{i} = -val;
else
value{i} = y;
end
end
dates = (cell2mat(value));
Mean_Anom = (((dates*2^(-23)))*pi);
Af0 = [];
fo = (strcat(valorsTramaBin_26,valorsTramaBin_27));
for example:
valorsTramaBin_15 = 1101 (32x4)
I want the four 0's before. Like 32x8 00001101

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 12 Dic 2012
valorsTramaBin_15 = dec2bin(valorsTrama(15,:), 8); %force 8 bits output

Più risposte (2)

Arthur
Arthur il 12 Dic 2012
Hi!
Try convert the data to a string array before you store in a variable

Walter Roberson
Walter Roberson il 12 Dic 2012
How exactly is the data being received? You say it is binary. Does that mean that for the value 0, it is sending 8 bits (one character) all filled with 0 bits, and for the value 1, it is sending 8 bits with the last of them 1? Or does it mean that for the value 0, it is sending the character '0' (decimal value 48) and for the value 1, it is sending the character '1' (decimal value 49) ?
At some point you appear to be trying to store the number in decimal form. You probably shouldn't be doing that.
If the difficulty you are running into is that there are not enough digits when you display the number or send it somewhere else, and the number is in decimal form, then use
sprintf('%08d', TheNumber')

Categorie

Scopri di più su Variables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by