plot a bar graph with 10 items and x and y labels in matlab

To plot a bar graph with 10 items and x and y labels in Matlab, follow these steps:

  1. Define a vector with the data to be plotted. For example:

    main.m
    data = [2 7 1 5 9 3 8 6 4 7];
    
    30 chars
    2 lines
  2. Use the bar function to create the bar graph:

    main.m
    bar(data);
    
    11 chars
    2 lines
  3. Use the xlabel function to add a label to the x-axis of the graph:

    main.m
    xlabel('Item number');
    
    23 chars
    2 lines
  4. Use the ylabel function to add a label to the y-axis of the graph:

    main.m
    ylabel('Value');
    
    17 chars
    2 lines
  5. (Optional) Use the title function to add a title to the graph:

    main.m
    title('My Bar Graph');
    
    23 chars
    2 lines

Here's the complete code:

main.m
data = [2 7 1 5 9 3 8 6 4 7];
bar(data);
xlabel('Item number');
ylabel('Value');
title('My Bar Graph');
104 chars
6 lines

This will create a bar graph with 10 items, labeled on the x-axis from 1 to 10, and with the values of the data vector plotted on the y-axis.

gistlibby LogSnag