Error using plot. Vectors must be the same lengths.

4 views (last 30 days)
Lukas
Lukas on 22 Sep 2014
Answered: dpb on 22 Sep 2014
Hi,
I want to generate something like a bar Chart with different lengths of the bars. I use this command:
plot(0:0, 0, 0:length1, vcur_1, length1:length2, vcur_2, ...
length2:length3, vcur_3, length3:length4, vcur_4, ...
length4:length5, vcur_5, length5:length6, vcur_6, ...
length6:length7, vcur_7, length7:length8, vcur_8, ...
length8:length9, vcur_9, length9:length10, vcur_10, ...
length10:length11, vcur_11, length11:length12, vcur_12, ...
length12:length13, vcur_13, length13:length14, vcur_14, ...
length14:length15, vcur_15, length15:length16, vcur_16, ...
length16:length17, vcur_17, length17:length18, vcur_18, ...
length18:length19, vcur_19, length19:length20, vcur_20)
I can choose in a GUI how many of the elements will be shown in the plot. It works for a few elements, but when I try to plot 20 elements, I get a Error: Error using plot Vectors must be the same lengths.
The variables are all the same lengths and i tried "clear" before the function but it doesn´t work. Does anybody know what I am doing wrong?
Lukas
  3 Comments
Andrew Reibold
Andrew Reibold on 22 Sep 2014
Edited: Andrew Reibold on 22 Sep 2014
This is scary Adam, but that's all one line of code... I'm not sure formatting would change it... Should probably split that up a bit Lukas, just for your own sake.
Adam
Adam on 22 Sep 2014
So it is...!!
Still, it is almost impossible to suggest much without some sample data though. If Matlab says the vectors are different lengths then they are so you must just be interpreting some of the lengths wrong, which is easy enough since all the x-vectors there are create in-place.
If I had to take a wild stab in the dark, without really knowing exactly what is in your length and vcur vectors I would suggest that the fact you start indexing from 0 and finishing at length20 may imply that somewhere there is an extra 1 value in your x vectors that isn't in the y vectors, but that is only a guess, without seeing what the variables actually contain.

Sign in to comment.

Answers (1)

dpb
dpb on 22 Sep 2014
...The variables are all the same lengths...
But, Matlab says they're not and Matlab doesn't lie (and rarely is such a case a bug in Matlab) so you need to change the thought process from thinking something's wrong in Matlab to finding out which one of the sets of variables is either mismatched from a typo or isn't the same length.
NB that "not same length" could be one of orientation of a row vector instead of column with the same number of elements as well as an actual different length.
Set debug on and go explore -- also as noted by others, converting to a set of arrays or cell arrays or even named-field structures could undoubtedly simplify this portion and much of your other coding as well by getting rid of all these redundant-named variables.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!