Asked by António Pereira
on 9 Jan 2019

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-->');

Answer by Adam Danz
on 10 Jan 2019

Edited by Adam Danz
on 10 Jan 2019

Accepted Answer

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)

Adam Danz
on 10 Jan 2019

Absolutely. Just replace the line #2 of my code with line #1.

António Pereira
on 10 Jan 2019

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
on 10 Jan 2019

Glad it helped!

Sign in to comment.

Answer by KSSV
on 10 Jan 2019

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-->');

António Pereira
on 10 Jan 2019

Hey, with that correction it still does give the same error

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Adam Danz (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/439011-vectors-must-be-the-same-length-error#comment_658782

## António Pereira (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/439011-vectors-must-be-the-same-length-error#comment_658822

## Adam Danz (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/439011-vectors-must-be-the-same-length-error#comment_659078

## António Pereira (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/439011-vectors-must-be-the-same-length-error#comment_659127

## Adam Danz (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/439011-vectors-must-be-the-same-length-error#comment_659159

## António Pereira (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/439011-vectors-must-be-the-same-length-error#comment_659191

Sign in to comment.