# to count number of bits in matlab

Status
Not open for further replies.

#### chitra ranganath

##### Member level 2
hi
i want to know how to calculate the total number of bits.

00000100 1 0 01000100 0 00111110 0 01000100 0 00000100 0 00000010

here i have the delimiter as space and more over i`ve stored this in a register z. pls help me out. i need matlab code

Quick&dirty

nbits = length(regexp(z,'\d'))

thank u sir one more doubt will same command give output for bytes in form
z=
0000100
1000001
1000000
0000100
1000001
1000000
0000100
1000001
1000000

z_ini = '00000100 1 0 01000100 0 00111110 0 01000100 0 00000100 0 00000010 ';

-----------------

Perhaps could have been:

z_ini = '00000100 1 0 01000100 0 00111110 0 01000100 0 00000100 0 00000010 '; % initial string

z_1 = regexprep(z_ini,' ','') ; % remove spaces
nbits = length(z_1) ; % bit count
z = strread(z_1,'%8s',-1) ; % Ascii byte array

Note:
It's a string array,

no sir the command which u gave is working properly and i obtained proper result also. but my doubt is if bits are like one below the other
z=
10010101
10101010
11110001
like this how to count the number of bits? bcos the command which u gave wil work only for string. so shd i convert these aray like appearance of bits to string as
z= 10010101, 10101010 ,11110001 and then count?????

I don't understand what you want to do, if that is the format, just write nbits = 8*length(z)

z=
10010101
10101010
11110001
i jus want to count number of bits in this.

Well, but wich is the format of z?
- String array of variable length?
- Integer array of fixed length ?
- Integer array of variable length starting with 1 ?
- Etc, etc ....

Status
Not open for further replies.