Search code examples
matlabgraphbar-chartmatlab-figure

Multiple discrete values in MATLAB graph


I have some data that I'd like to plot on a graph in MATLAB. the data is discrete - specifically these are 2 series of data against a single vector. I could easily do it in excel like this:

enter image description here

but I want to do it in matlab. I tried to use the stem function, but the 2 series values are shown on the same bar (and I want them side by side, like the excel does):

enter image description here

In addition, I would like to show on the x bar only the values I'm interested in (in my case: 2,4,8,16,32). How do I do that?


Solution

  • Since you want to draw a bar graph, there is a dedicated built-in function, named bar(), for that purpose.

    You can do it using:

    N = [2 4 8 16 32];
    val1 = [1; 2; 3; 4; 5];
    val2 = [3; 5; 6; 12; 17];
    bar([N],[val2,val1]);   % If you want val1 to appear first then use bar([N],[val1,val2]);
    

    which gives the following desired result:

    output