how can i avoid return to zero from my plot

8 views (last 30 days)
Bitsnum = 11;
dataIn = randi([0 1],1,Bitsnum);
i = 1;
count = 0;
t = 0:.01:length(dataIn);
for j =1:length(t)
if t(j) >= (0+count) && t(j) <= (0.5+count);
if dataIn(i) == 1;
manchester(j) = 2;
elseif dataIn(i) == 0;
manchester(j) = -2;
end
elseif t(j) > (.5+count) && t(j) <= (i);
if dataIn(i) == 1;
manchester(j) = -2;
elseif dataIn(i) == 0;
manchester(j) = 2;
end
else
i = i+1;
count = count+1;
end
end
subplot(3,2,5);
plot(t,manchester,'LineWidth',2,'color','red');
axis([0 length(dataIn) -3 3])
grid on;
title(['manchester: [' num2str(dataIn) ']']);

Accepted Answer

Kodavati Mahendra
Kodavati Mahendra on 24 Mar 2019
Bitsnum = 11;
dataIn = randi([0 1],1,Bitsnum);
i = 1;
count = 0;
t = 0:.01:length(dataIn);
for j =1:length(t)
if t(j) >= (0+count) && t(j) <= (0.5+count);
if dataIn(i) == 1;
manchester(j) = 2;
elseif dataIn(i) == 0;
manchester(j) = -2;
end
elseif t(j) > (0.5+count) && t(j) <= (i);
if dataIn(i) == 1;
manchester(j) = -2;
elseif dataIn(i) == 0;
manchester(j) = 2;
end
else
manchester(j)= manchester(j-1);
i = i+1;
count = count+1;
end
end
% subplot(3,2,5);
plot(t,manchester,'LineWidth',2,'color','red');
axis([0 length(dataIn) -3 3])
grid on;
title(['manchester: [' num2str(dataIn) ']']);
I am not sure if i understand your question properly. Does this code solve your problem?
The problem is with your ifelse loop. for some j, manchester(j) is undefined. MATLAB assigned it a default value of 0. So I added one more line inside else. Does this work?
  2 Comments
Kodavati Mahendra
Kodavati Mahendra on 24 Mar 2019
for example, check the output of the following command to understand the problem with your code.
x(1) = 1;
x(10) = 1;
plot(x);
MATLAB doesn't know the values of x(2) to x(9). So it sets them to 0 and plots the graph. If you want them to be something different, you should declare them properly.
Maybe
x(1:10) = 1;plot(x);

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!