Hello everyone, im facing a problem that i cant solve it. Im new to MatLab and im having a , Vectors must be the same length, error. I dont know that well about grafics in matlab so if someone could help me it would be apreciated
heres my code
bny = input('Insira o codigo binario a codificar: ' ,'s');
idx = ismember(bny,'01');
assert(all(idx),'O codigo binario só pode conter 0s e 1s, mas contem o(s) número(s) %s',bny(~idx))
fprintf('O seu codigo é: %s\n',bny)
V = [-3,3];
n = V(bny-'/');
i=1;
a=0;
b=0.5;
t=0:0.01:length(bny);
for j=1:length(bny)
if t(j)>=a && t(j)<=b
y(j)=V(i);
elseif t(j)>b && t(j)<=i
y(j)=0;
else
i= i+1;
a=a+1;
b=b+1;
end
end
plot(t,y,'k');axis([0 length(bny) -5 5]);title('Rz Polar');
xlabel('time-->');
ylabel('Amplitude-->');

댓글 수: 6

Adam Danz
Adam Danz 2019년 1월 9일
편집: Adam Danz 2019년 1월 9일
What input to 'bny' produces the error?
When I input 0 or 1 there is no error.
Also, copy-paste the full error message.
António Pereira
António Pereira 2019년 1월 9일
rz
Insira o codigo binario a codificar: 100110
O seu codigo é: 100110
Error using plot
Vectors must be the same length.
Error in rz (line 23)
plot(t,y,'k');axis([0 length(bny) -5 5]);title('Rz Polar');
For input '100110', the variable 't' (below) will be a vector with 601 elements.
t=0:0.01:length(bny); %length(bny) = 6
The for-loop (below) will have 6 iterations so the variable 'y' created within the for-loop will be a vector of 6 elements.
for j=1:length(bny) %length(bny) = 6
...
end
Then you're trying to plot y as a function of t (below) and to do this, 'y' and 't' need to have the same ammount of data. But your code produces 601 data points for 't' and only 6 data points for 'y' (for the inputs 100110)
plot(t,y,'k')
I haven't gone through your code to understand what you're trying to do but this explains the source of the error.
António Pereira
António Pereira 2019년 1월 10일
편집: António Pereira 2019년 1월 10일
I think i got it, but im new to matlab so i dont know that well how to do that
Adam Danz
Adam Danz 2019년 1월 10일
편집: Adam Danz 2019년 1월 10일
Can you explain the purpose of the code, the inputs and expected outputs?
António Pereira
António Pereira 2019년 1월 10일
Sure.The purpose is to make a simulation of a RZ coded digital signal. The input is to add the 1´s and 0´s (binary) that will be coded. The expected output would be a grafic that when value is "1" it would go y=3 (and then halfway point it would go down to the middle again) i will show a image of that down below.

댓글을 달려면 로그인하십시오.

 채택된 답변

Adam Danz
Adam Danz 2019년 1월 10일
편집: Adam Danz 2019년 1월 10일

1 개 추천

After understanding the purpose of the code in the comments under the question, I rewrote the code to produce a plot that was described above. If this isn't what you were looking for or if you have specific quesitons about your original code, I'd be glad to help more.
%bny = input('Insira o codigo binario a codificar: ' ,'s');
bny = '100110';
bvec = bny-'0'; %convert to numerical vector
% create x,y values of step function
stepHeight = 3;
stepIdx = 0 : 0.5 : length(bvec)-0.5;
stepX = repelem(stepIdx, 2);
stepY = repmat([0, stepHeight, stepHeight, 0], 1, length(bvec));
% now we have a step function that's all positive
% figure
% plot(stepX, stepY, 'r-')
% ylim([-stepHeight, stepHeight]*2)
% flip sign of steps associated with bny=0
zeroIdx = repelem(bvec == 0, 4); %4 because there are 4 values in stepX/Y for each step
tallIdx = stepY > 0;
stepY(zeroIdx & tallIdx) = -1 * stepHeight;
% plot results
figure
plot([min(stepX), max(stepX)], [0,0], 'k-', 'LineWidth', 4) %reference line at y = 0
hold on
plot(stepX, stepY, 'r-', 'LineWidth', 3) %step function
ylim([-stepHeight, stepHeight]*2)

댓글 수: 4

António Pereira
António Pereira 2019년 1월 10일
Thank you so much. It works great. But my question if it is possible to be de user to chosse the 0's and 1's insted of doing it on the code.
Adam Danz
Adam Danz 2019년 1월 10일
Absolutely. Just replace the line #2 of my code with line #1.
António Pereira
António Pereira 2019년 1월 10일
편집: António Pereira 2019년 1월 10일
Wow, this is working really nice. Thank you so much!!! Priciate your time.
Just one last thing. If i wanted to block the user from using other numbers (not allow him to use numbers besides 0 and 1) how would i do it?
Adam Danz
Adam Danz 2019년 1월 10일
Glad it helped!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2019년 1월 10일

0 개 추천

YOu need to rethink on your code.
bny = input('Insira o codigo binario a codificar: ' ,'s');
idx = ismember(bny,'01');
assert(all(idx),'O codigo binario só pode conter 0s e 1s, mas contem o(s) número(s) %s',bny(~idx))
fprintf('O seu codigo é: %s\n',bny)
V = [-3,3];
n = V(bny-'/');
i=1;
a=0;
b=0.5;
% t=0:0.01:length(bny);
t = linspace(0,length(bny),length(bny)) ;
for j=1:length(bny)
if t(j)>=a && t(j)<=b
y(j)=V(i);
elseif t(j)>b && t(j)<=i
y(j)=0;
else
i= i+1;
a=a+1;
b=b+1;
end
end
plot(t,y,'k');axis([0 length(bny) -5 5]);title('Rz Polar');
xlabel('time-->');
ylabel('Amplitude-->');

댓글 수: 1

António Pereira
António Pereira 2019년 1월 10일
Hey, with that correction it still does give the same error

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

태그

질문:

2019년 1월 9일

편집:

2019년 1월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by