MATLAB Answers

how can i avoid return to zero from my plot

조회 수: 9(최근 30일)
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) ']']);

  댓글 수: 0

로그인 to comment.

채택된 답변

Kodavati Mahendra
Kodavati Mahendra 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

Kodavati Mahendra
Kodavati Mahendra 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);
Mohamed Essam
Mohamed Essam 24 Mar 2019
thank you very much

로그인 to comment.

추가 답변(0개)

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by