Adding Labels to a bar graph (2025)

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 Comments

Show NoneHide None

Adding Labels to a bar graph (3)

the cyclist on 27 Jul 2011

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

Sign in to comment.

Adding Labels to a bar graph (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6662

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.