# Error using bar: inputs must be 2-D

2 ビュー (過去 30 日間)
Guillermo 2023 年 11 月 9 日
コメント済み: Guillermo 2023 年 11 月 9 日
I'm taking a class on Matlab, and for my first homework I need to make bar graphs, separating candidates by gender on one graph and by colleges on the other. I must compare their average salaries. I was able to make the first bar graph, however, I did almost the exact same for the second graph (the one sorted by colleges), but Matlab refuses, citing the error; "Error using bar: inputs must be 2-D". Here's the code:
% Excercise 1 %
salary0=univ0.salary;
salary1=univ1.salary;
gender=[0, 1];
meanSalary=zeros(1,2);
meanSalary(1)=mean(salary0);
meanSalary(2)=mean(salary1);
bar(gender,meanSalary)
% Excercise 2 %
c_salary1=c_univ1.salary;
c_salary2=c_univ2.salary;
c_salary3=c_univ3.salary;
c_salary4=c_univ4.salary;
c_salary5=c_univ5.salary;
c_salary6=c_univ6.salary;
c_salary7=c_univ7.salary;
college=[1,2,3,4,5,6,7];
c_meanSalary=zeros(1,2,3,4,5,6,7);
c_meanSalary(1)=mean(c_salary1);
c_meanSalary(2)=mean(c_salary2);
c_meanSalary(3)=mean(c_salary3);
c_meanSalary(4)=mean(c_salary4);
c_meanSalary(5)=mean(c_salary5);
c_meanSalary(6)=mean(c_salary6);
c_meanSalary(7)=mean(c_salary7);
bar(college,c_meanSalary)

サインインしてコメントする。

### 採用された回答

Stephen23 2023 年 11 月 9 日

The command zeros(1,2,3,4,5,6,7) creates a 7D array with size 1x2x3x4x5x6x7.
What you need is a 2D matrix:
zeros(1,7)
Tip: the first thing to do when debugging code is look at what your code is actually doing. A really good start is to look at all of the variables: what values do they have, what classes, what sizes, etc. If you had done this, you would have noticed that c_meanSalary is a huge array with seven dimensions and over five thousand elements.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Guillermo 2023 年 11 月 9 日
I understand, catching these little things is hard for a newbie. Appreciate the help!

サインインしてコメントする。

### カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by