Error using Bar, X must be same Length as Y.

조회 수: 72 (최근 30일)
Matt Glowacki
Matt Glowacki 2019년 6월 24일
댓글: Adam Danz 2020년 5월 25일
I am trying to plot on a bar chart through app designer and i'm getting the following error: Error using bar (line 172)
X must be same length as Y.
My code is as below:
bar(app.UIAxesPositionBar, T_BarChart.UndlyDate, T_BarChart{:,{'TotalPosition'}}, 0.8,'FaceAlpha',0.5);
hold(app.UIAxesPositionBar,'on');
bar(app.UIAxesPositionBar, T_BarChart.UndlyDate, T_BarChart{:,{'FuturesPosition','DeltaEquivPosition'}}, 0.8,'FaceAlpha', 0.5);
The second call to bar throws the error, when i inspect the table i'm using you can see its 1x4, the first call to bar works just fine.
You can see the table contents/variables below:
K>> bar(app.UIAxesPositionBar, T_BarChart.UndlyDate, T_BarChart{:,{'FuturesPosition','DeltaEquivPosition'}}, 0.8,'FaceAlpha', 0.5);
Error using bar (line 172)
X must be same length as Y.
K>> T_BarChart.UndlyDate
ans =
datetime
01-Dec-2019
K>> T_BarChart{:,{'FuturesPosition','DeltaEquivPosition'}}
ans =
1.0e+02 *
0 -1.887165000000000
K>> T_BarChart
T_BarChart =
1×4 table
UndlyDate FuturesPosition DeltaEquivPosition TotalPosition
___________ _______________ __________________ _____________
01-Dec-2019 0 -188.7165 -188.7165
Any ideas on why i am getting the "must be the same length" error?
  댓글 수: 4
Nyaz Jamel
Nyaz Jamel 2020년 5월 25일
Note : Sample value=50, upsampling=2 input signal freq=1500
Adam Danz
Adam Danz 2020년 5월 25일
With
stem(app.UIAxes, X, Y)
length(X) must equal length(Y). According to your error message, this is not the case for your values. It makes sense, right? X and Y are pairs of values so each X needs to have a Y value and vise versa.

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

채택된 답변

Adam Danz
Adam Danz 2019년 6월 24일
편집: Adam Danz 2019년 9월 30일
As the error indicates, x and y must be the same length. Here's a demo to understand the error:
bar(1,2) % this is OK
bar([1,2,3],[1 1 1;2 2 2]) % this is also OK because
% length([1,2,3]) = 3 and length([1 1 1;2 2 2]) = 3
bar(1,[1,2]) % this produces an error
Error using bar (line 172)
X must be same length as Y.
One solution is to specify two x values
bar([1,2], [4,5])
Another solution is to not specify the x values at all. It looks like one column is a single value while the other column might be two stacked values. In that case you can do something like this:
bar([0 5; 2 3],'stacked')
Read more about inputting matrices in bar(): https://www.mathworks.com/help/matlab/ref/bar.html
  댓글 수: 7
Ulises Alejandro Aregueta Robles
But then why this works with the example provided by MatLab documentation? (as below)
x = [1980 1990 2000];
y = [15 20 -5; 10 -17 21; -10 5 15];
bar(x,y,'stacked')
this is the example the matlab gives to plot stacked bars and here x is not the same length as y.
Adam Danz
Adam Danz 2019년 9월 30일
편집: Adam Danz 2019년 9월 30일
x and y are the same length.
x = [1980 1990 2000];
y = [15 20 -5; 10 -17 21; -10 5 15];
>> length(x)
%ans =
% 3
>> length(y)
%ans =
% 3
The length(x) function returns the length of the largest array dimension in X. In the data above, the size of x is 1x3 and the size of y is 3x3.
I updated my answer to provide an example of that, too.

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

추가 답변 (2개)

Matt Glowacki
Matt Glowacki 2019년 6월 24일
Thank you very much for your response! Perhaps I can provide a better illustration with this example code:
bar([1;2], [3;5],'FaceAlpha',.5)
hold on
bar([1;2], [1,2;3,2],'FaceAlpha',.5)
hold off
What I am trying to show is a total position and how the 2 pieces aggregate to that position. When my x variable has more than one value like above, my code works as I expect for each underlying date. What I'm looking for is to only have one side of the above graph only (like in my example above, my table only has one row, i want to add the first "total" bar, then just like the above code add the two bars on top of the first).
Is there another way to make sure that matlab knows that my code is passing 1 row with two variables in my original code above?
Thanks again for your help.
  댓글 수: 1
Adam Danz
Adam Danz 2019년 6월 25일
Please reply in the comment section under my answer.

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


Matt Glowacki
Matt Glowacki 2019년 6월 25일
So i was able to get mostly what I want by appending another row with 0's for each of the respective columns for one month after the single row in the table I was using. This doesnt look perfect as half the graph is blank, but its better than no graph. This is the table before and after modifying by the code below:
K>> T_BarChart
T_BarChart =
1×4 table
UndlyDate FuturesPosition DeltaEquivPosition TotalPosition
___________ _______________ __________________ _____________
01-Sep-2019 0 -305.428 -305.428
K>> T_BarChart
T_BarChart =
2×4 table
UndlyDate FuturesPosition DeltaEquivPosition TotalPosition
___________ _______________ __________________ _____________
01-Sep-2019 0 -305.428 -305.428
01-Oct-2019 0 0 0
dte = T_BarChart.UndlyDate(1);
T_BarChart = [T_BarChart; {datetime(year(dte),month(dte)+1,1),0,0,0}];
bar(app.UIAxesPositionBar, T_BarChart.UndlyDate, T_BarChart{:,{'TotalPosition'}}, 0.8,'FaceAlpha',0.5);
hold(app.UIAxesPositionBar,'on');
bar(app.UIAxesPositionBar, T_BarChart.UndlyDate, T_BarChart{:,{'FuturesPosition','DeltaEquivPosition'}}, 0.8,'FaceAlpha', 0.5);
  댓글 수: 1
Adam Danz
Adam Danz 2019년 6월 25일
Please reply in the comment section under my answer.

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

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by