What does "Vectors must be the same lengths." mean?

I have been trying to adjust the code to have different values but it keeps messing up and I can't get anything to work. Here was the code:
clear; load testtrace.mat
subplot(2,1,1);
plot(t,tracefar)
title('1000 m offset');xlabel('seconds')
subplot(2,1,2);
plot(t,tracenear)
title('10 m offset');xlabel('seconds')
envfar = abs(hilbert(tracefar)); %compute Hilbert envelope
envnear = abs(hilbert(tracenear)); %compute Hilbert envelope
envdbfar=todb(envfar,max(envnear)); %decibel conversion
envdbnear=todb(envnear); %decibel conversion
figure
plot(t,[envdbfar envdbnear],'b');xlabel('seconds');ylabel('decibels');
grid;axis([0 3 -140 0])
and here is the error code that shows up:
>> Untitled2
Error using plot
Vectors must be the same lengths.
Error in Untitled2 (line 3)
plot(t,tracefar)

 Accepted Answer

Use the command
whos
to check the dimensions of all the variables.
Your error flag indicates that size(t) and size( tracefar) are not compatible for
plot(t, tracefar)
Hope this helps.
Thank you for formally accepting my answer
Greg

More Answers (1)

I'm guessing that there's a variable in testtrace.mat called t, and another variable called tracefar. If you type length(t) into the command window, and then type length(tracefar) into the command window, you'll see that they are different numbers.
When you tell matlab to plot t and tracefar, Matlab needs to plot one y value (tracefar) for each x value (t). If x and y (or t and tracefar) do not have the same number of values, Matlab will get confused.

2 Comments

Excellent, how we can make the same two different dimension variables? for example t=101 and E=102
L = min(length(t), length(E));
plot(t(1:L), E(1:L))

Sign in to comment.

Products

Tags

Community Treasure Hunt

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

Start Hunting!