-1

I have the following code:

  mean_h =[11.3579,9.8330,13.4154];
   std_h = [11.3427,6.8093,9.4281];
   figure
   hold on
   h = bar(1:3,mean_h);
   errorbar(1:3,mean_h,std_h,'s');

Now I am getting 3 bars with error bars on it . I want to label each bar along x axis with separate name: like bar 1 with 4 , bar 2 with 6 and bar 3 with 8.

How can I do that in MATLAB with code?

0

1 Answer 1

0

Use XTick to define where you want labels, and XTickLabel to change the name, so in your case:

mean_h =[11.3579,9.8330,13.4154];
std_h = [11.3427,6.8093,9.4281];
figure
hold on
h = bar(1:3,mean_h);
errorbar(1:3,mean_h,std_h,'s');                    
set(gca,'XTick', 1:3);
set(gca,'XTickLabel',{'4','6','8'});

Or for versions R2016b and younger you can also use

mean_h =[11.3579,9.8330,13.4154];
std_h = [11.3427,6.8093,9.4281];
figure
hold on
h = bar(1:3,mean_h);
errorbar(1:3,mean_h,std_h,'s');                    
xticks(1:3);
xticklabels({'4','6','8'});
7
  • Thanks. I really works . I have one more question. If i want to show the legend of the bar and also error bar how can i do that . Can you help me with code? Commented Feb 12, 2019 at 13:29
  • 2
    When you know that it's a duplicate, why did you still post the answer? Commented Feb 12, 2019 at 13:29
  • I am sorry for again posting. Can you help me with code regarding legends of bar and error bar Commented Feb 12, 2019 at 13:32
  • @AhmadBilal you can use legend for that
    – ViG
    Commented Feb 12, 2019 at 13:35
  • 2
    Which means that you should post your answer there; not here Commented Feb 12, 2019 at 13:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.