So I think you missed that dec2bin only works with unsigned numbers.
Here is my version which does a Two's complement conversion after generating a unsigned sine with offset
Note: Syntax like ++ only works with octave... if you use matlab, you need to do something like x=x+1;
clear all;
close all;
fig=1;
nos=256;
amp=1;
t=0:(2.*pi)./(nos.-1):(2.*pi);
sine_w=amp.*sin(t);
figure(fig++);
plot(t,sine_w);
grid on;
number_of_bits=8;
ampl_pp = 2.*amp;
lsb = ((2.^number_of_bits)-1)./ampl_pp;
sine_discrete_signed_tmp = (sine_w.*lsb);
sine_discrete_signed = int8(sine_discrete_signed_tmp);
max(sine_discrete_signed_tmp)
min(sine_discrete_signed_tmp)
figure(fig++);
plot(t,sine_discrete_signed);
grid on;
sine_discrete_unsigned_tmp = ((1.+sine_w).*lsb);
sine_discrete_unsigned = uint8(sine_discrete_unsigned_tmp);
max(sine_discrete_unsigned)
min(sine_discrete_unsigned)
figure(fig++);
plot(t,sine_discrete_unsigned);
grid on;
length = size(sine_discrete_unsigned,2);
sine_unsigned_2thcompl = zeros(1,length);
for(n=1:1:length)
if(0 <= sine_discrete_signed(:,n))
sine_unsigned_2thcompl(:,n) = sine_discrete_unsigned(:,n) .- (2^(number_of_bits.-1));
else
sine_unsigned_2thcompl(:,n) = (2^(number_of_bits.-1)) .+ sine_discrete_unsigned(:,n);
endif
endfor
max(sine_unsigned_2thcompl)
min(sine_unsigned_2thcompl)
figure(fig++);
plot(t,sine_unsigned_2thcompl);
grid on;
yy=(dec2bin(sine_unsigned_2thcompl));
fid=fopen('sine_value.data','wt');
for(n=1:1:size(yy,1))
fprintf(fid, '%s \n', yy(n,:));
endfor
fclose(fid);
display('text file for signal finished');

with only 6 Bits and a stairplot the discrete steps are clearly visible
