Setting a limit on decimal points for a bar graph

I would like to make it so my decimals only go out to the hundredths place in my bar graphs, but I am not sure how to go about doing this. Any help would be much appreciated. Below is an example of the script for one of my bar graphs and the corresponding graph.
Titan_CO = interp1(Titan_SHP_Load,Titan_CO_Load,4384);
T4F_CO = 0.224;
T2_CO = 3.5;
T4F_Dual_Fuel_CO = (T4F_Dual_Fuel_SHP-1250)/625*0+0.00974545454545454;
Nat_Gas_Recip_CO = (Nat_Gas_Recip_SHP-2607)/860*(2.03835322515757-1.94448169505163)+1.94448169505163;
Large_Turbine_CO = 0.09;
T2_Dual_Fuel_CO = (T2_Dual_Fuel_SHP-1250)/625*(0.606644827008-0.199511534016)+0.199511534016;
CO = [Titan_CO, T4F_CO, T2_CO, T4F_Dual_Fuel_CO, Nat_Gas_Recip_CO, Large_Turbine_CO, T2_Dual_Fuel_CO];
figure3 = figure;
b = bar(CO,'FaceColor',[0.4660 0.6740 0.1880]);
ylim([0 4]);
xtips = b(1).XEndPoints;
ytips = b(1).YEndPoints;
labels = string(b(1).YData);
text(xtips,ytips,labels,'HorizontalAlignment','center','VerticalAlignment','bottom')
set(gca,'XTickLabel',{'Titan', 'T4F', 'T2', 'T4F Dual Fuel', 'Nat Gas Recip', 'Large Turbine Gen', 'T2 Dual Fuel'})
grid on
xlabel('Engine Type','FontSize',10,'FontWeight','bold');
ylabel('CO Emissions Rate (g/kw-hr)','FontSize',10,'FontWeight','bold');

 Accepted Answer

You can use sprintf('%.2f',__) or sprintfc('%.2f',__), as in:
CO = 4*rand(1,7);
figure3 = figure;
b = bar(CO,'FaceColor',[0.4660 0.6740 0.1880]);
ylim([0 4]);
xtips = b(1).XEndPoints;
ytips = b(1).YEndPoints;
% labels = string(b(1).YData);
labels = sprintfc('%.2f',b(1).YData);
text(xtips,ytips,labels,'HorizontalAlignment','center','VerticalAlignment','bottom')
set(gca,'XTickLabel',{'Titan', 'T4F', 'T2', 'T4F Dual Fuel', 'Nat Gas Recip', 'Large Turbine Gen', 'T2 Dual Fuel'})
grid on
xlabel('Engine Type','FontSize',10,'FontWeight','bold');
ylabel('CO Emissions Rate (g/kw-hr)','FontSize',10,'FontWeight','bold');

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2021a

Tags

Asked:

on 29 Mar 2022

Commented:

on 29 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!