Error using plot. Vectors must be the same lengths.

조회 수: 3 (최근 30일)
Lukas
Lukas 2014년 9월 22일
답변: dpb 2014년 9월 22일
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
Andrew Reibold
Andrew Reibold 2014년 9월 22일
편집: Andrew Reibold 2014년 9월 22일
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 2014년 9월 22일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

dpb
dpb 2014년 9월 22일
...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.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by