Adding Labels to a bar graph
236 次查看(过去 30 天)
显示 更早的评论
Hey everyone, I am a student learning how to use matlab. I cannot find out to add labels to my bar graphs in my text or matlab's help section.
currently my code is:
bar(years,per_year_growth)
title('Total forest')
xlabel('Years')
ylabel('Acres Per Year')
This works for most plots but not the bar graph. Any help is appreciated. Thanks.
EDIT:
Posting my full program so this is easier to understand.
years=0:20;
total_forest = zeros(21,1);
per_year_growth = zeros(21,1);
total=14000;
uncut=2500;
rate=0.02;
output = zeros(3,21);
total_forest(1) = uncut;
for n = 1:20
total_forest(n+1) = uncut*((1+rate)^n);
end
per_year_growth(1) = 0;
for n = 1:20
per_year_growth(n+1) = total_forest(n+1) - total_forest(n);
end
output(1,:) = years;
output(2,:) = total_forest;
output(3,:) = per_year_growth;
disp('Years Total Acres Acres Grown Per Year')
fprintf('%f %f %f\n',output)
bar(years,per_year_growth)
title('Total forest')
xlabel('Years')
ylabel('Acres Per Year')
The specific error I get is:
??? Index exceeds matrix dimensions.
Error in ==> hmwk9_pace at 69
xlabel('Years')
Line 69 is "xlabel('Years')" w/o quotes of course.
2 个评论
the cyclist
2011-7-27
The full code you posted works perfectly well for me. I suggest you try restarting MATLAB, and see if that helps.
Also, maybe try "which xlabel" to make sure you don't have some function of your own lurking somewhere, that you defined. The function should be in your_matlab_directory/toolbox/matlab/graph2d/xlabel.m
回答(4 个)
the cyclist
2011-7-27
This worked for me:
years = rand(3,4);
bar(years)
title('Total forest')
xlabel('Years')
ylabel('Acres Per Year')
0 个评论
Fangjun Jiang
2011-7-27
I ran your code without any problem. The error message indicates that you might have a variable called xlabel. Check your worksapce.
>> xlabel=1:10
xlabel =
1 2 3 4 5 6 7 8 9 10
>> xlabel('Years')
??? Index exceeds matrix dimensions.
>> which xlabel -all
xlabel is a variable.
C:\Program Files\MATLAB\R2010b\toolbox\matlab\graph2d\xlabel.m % Shadowed
0 个评论
Paulo Silva
2011-7-27
I works just fine, you might be doing something else that's affecting the axes, try to run it after
cla reset
Also try this code
x = -2.9:0.2:2.9;
bar(x,exp(-x.*x),'r')
title('Total forest')
xlabel('Years')
ylabel('Acres Per Year')
0 个评论
Stenila Simon
2018-8-31
For anyone out there in the future looking for a solution, another way to do it is to right-click on the "xlabel" in your code, select "open xlabel", then go to the list of variables, right click again and delete. That deletes the existence of xlabel as a variable anywhere in your system.
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!