# Error 'vectors must be all the same lengths'

9 views (last 30 days)
john on 11 Jan 2015
Edited: John D'Errico on 11 Jan 2015
Error 'vectors must be all the same lengths, i am trying to plot multiple lines on a graph without much success, i input (below) and Error 'vectors must be all the same lengths'is the answer any help would be appreciated.
>> temp1=[13.719, 13.886, 13.909, 13.916, 13.907, 13.919];
>> depth1=[1.8, 4.0, 6.0, 8.5, 10.5, 12.4];
>> plot(temp1,-depth1)
>> temp2=[13.796, 13.863, 13. 865, 13.893, 13.881];
>> depth2=[1.8, 3.9, 5.9, 8.4, 10.4];
>> hold on;
>> plot(temp2,-depth2)
Error using plot
Vectors must be the same lengths.

John D'Errico on 11 Jan 2015
Edited: John D'Errico on 11 Jan 2015
I'd lay large odds the problem is here:
temp2=[13.796, 13.863, 13. 865, 13.893, 13.881];
..........................^.....................
That space between 13. and 865 makes it a vector of length 6, not 5. Of course, it is clear that the third element was really intended as 13.865, just in terms of smoothness of the vector.
How do you expect plot to work when the vectors are not compatible in length? The error message was a clue. Look carefully at the vectors. Dump them to the command window. At the very least, look at the output of whos. These things will help you to find the typo.

Geoff Hayes on 11 Jan 2015
John - the error is occurring because the vector temp2 has 6 elements whereas the vector depth2 has only five elements. Since you are trying to plot temp2 versus depth2, both of these vectors must have the same number of elements (like in your first example).
If you compare the two depth vectors, you will note that the second one is missing an element around 12.4. Review thisvector and see what value is missing, correct it, and try the plot again.