Vectors must be the same length.

%Signal generator code
disp('Welcome to our signal generation simple application')
sf= input('Please enter sampling frequency:');%sampling frequency
start= input('Please enter signal start point:');%start point
ending= input('Please enter signal end point:');%end point %end is a key word so we couldn't use it as a variable
bp= input('Please enter number of break points');%break points
bpvec=zeros(1,bp);%breaking points vector
if bp~=0
t=linspace(start,ending,(ending-start)*sf);
for i=1:1:bp
bpvec(i)=input('please enter the position of the breaking point');
t1=linspace(start,bpvec(i),(bpvec(i)-start)*sf);
type= menu('type of the sigal partition','Impulse signal','DC signal','Ramp signal','Exponential','Sinusoidal');%type of each partition
switch type
case 1
area=input('please enter the area');
x=zeros(1,length(t1));
x(1)=area;
y1=x;
case 2
amp=input('please enter the amplitude');
x=amp*(ones(1,length(t1)));
y1=x;
case 3
slope=input('please enter the slope');
intercept=input('please enter the intercept');
y1=slope*t1+intercept;
case 4
amp=input('please enter the amplitude');
ex=input('please enter the exponent');
y1=amp*exp(t1*ex);
case 5
amp=input('please enter the amp');
freq=input('please enter the frequency');
phase=input('please enter the phase');
y1=amp*sin((2*pi*freq*t1)+phase);
end
start=bpvec(i);
if i==1
y=y1;
else
y=[y y1];
end
y=[y y1];
end
else %if no break points
t=linspace(start,ending,(ending-start)*sf);
type= menu('Impulse signal','DC signal','Ramp signal','Exponential','Sinusoidal');%type of each partition
switch type
case 1
area=input('please enter the area');
x=zeros(1,length(t));
x(1)=area;
y1=x;
case 2
amp=input('please enter the amplitude');
x=amp.*(ones(1,length(t)));
y1=x;
case 3
slope=input('please enter the slope');
inttercept=input('please enter the intercept');
y1=slope*t+intercept;
case 4
amp=input('please enter the amplitude');
ex=input('please enter the exponent');
y1=amp*exp(t*ex);
case 5
amp=input('please enter the amp');
freq=input('please enter the frequency');
phase=input('please enter the phase');
y1=amp*sin((2*pi*freq*t)+phase);
end
y=y1;
end
plot(t,y)

3 个评论

please show the complete error message
Error using plot.
vectors must be the same length
error in miniproject1 line 82
plot(t,y)
what is the
sequence of inputs that trigger the error?

请先登录,再进行评论。

回答(2 个)

You have
if i==1
y=y1;
else
y=[y y1];
end
y=[y y1];
Every iteration of the for i loop, that adds y1 to the end of y twice, so y will be length 2*bp . But you have
t=linspace(start,ending,(ending-start)*sf);
which is a length that is unrelated to bp.

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by