Maybe it's too later, but:
1. For extract probability use 'hist' function: p = hist(signal) or p = hist(signal, lengths)
2. Don't need that long code for entropy! I think it's wrong, because is 'log2(x(i))' and not 'log2(1/x(i))'. But really better you replace ALL and use only: -sum(p.*log2(p))
Good Luck!