Error using bar: inputs must be 2-D

7 Ansichten (letzte 30 Tage)
Guillermo
Guillermo am 9 Nov. 2023
Kommentiert: Guillermo am 9 Nov. 2023
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 %
univ1 = UniversityofFloridagraduatesalaries(UniversityofFloridagraduatesalaries.gender==1,:)
Unrecognized function or variable 'UniversityofFloridagraduatesalaries'.
univ0 = UniversityofFloridagraduatesalaries(UniversityofFloridagraduatesalaries.gender==0,:)
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_univ1 = UniversityofFloridagraduatesalaries(UniversityofFloridagraduatesalaries.college==1,:)
c_univ2 = UniversityofFloridagraduatesalaries(UniversityofFloridagraduatesalaries.college==2,:)
c_univ3 = UniversityofFloridagraduatesalaries(UniversityofFloridagraduatesalaries.college==3,:)
c_univ4 = UniversityofFloridagraduatesalaries(UniversityofFloridagraduatesalaries.college==4,:)
c_univ5 = UniversityofFloridagraduatesalaries(UniversityofFloridagraduatesalaries.college==5,:)
c_univ6 = UniversityofFloridagraduatesalaries(UniversityofFloridagraduatesalaries.college==6,:)
c_univ7 = UniversityofFloridagraduatesalaries(UniversityofFloridagraduatesalaries.college==7,:)
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)
Note: UniversityofFloridagraduatesalaries is the Excel spreadsheet I imported into a table.

Akzeptierte Antwort

Stephen23
Stephen23 am 9 Nov. 2023
Bearbeitet: Stephen23 am 9 Nov. 2023
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 Kommentar
Guillermo
Guillermo am 9 Nov. 2023
I understand, catching these little things is hard for a newbie. Appreciate the help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by