how can i avoid return to zero from my plot

6 Ansichten (letzte 30 Tage)
Mohamed Essam
Mohamed Essam am 24 Mär. 2019
Kommentiert: Mohamed Essam am 24 Mär. 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) > (.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) ']']);

Akzeptierte Antwort

Kodavati Mahendra
Kodavati Mahendra am 24 Mär. 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 Kommentare
Kodavati Mahendra
Kodavati Mahendra am 24 Mär. 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 am 24 Mär. 2019
thank you very much

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by