adding textbox to plot - text doesnt stay in one line

15 views (last 30 days)
Hi,
i want to add a text box to my chart. I followed the documentation but in my case, the text doesnt stay in one line and I'm wondering how I can resolve this.
It should be 'Biomass = 1.03' and 'Electricity = 2.065'.
figure()
hb = bar(out.combinednew{:,1}, [out.combinednew{:,3},out.combinednew{:,9},out.combinednew{:,12}]);
legend(out.combinednew.Properties.VariableNames([3,9,12]));
xlabel('Primary Energy Demand');
xscale = Tmin:2:Tmax;
xticks(Tmin-2:2:Tmax+2);
xticklabels({'HP',xscale,'LGB'})
hb(1).FaceColor = [0.83 0.83 0.83];
hb(2).FaceColor = [0.4660 0.6740 0.1880];
hb(3).FaceColor = [0 0.4470 0.7410];
txt = {'Primary energy factors:','Biomass =' num2str(Basisdaten.Primaerenergiefaktor.Biomasse.today), ...
'Electricity =' num2str(Basisdaten.Primaerenergiefaktor.Strom.today),};
dim = [.15 .6 .3 .3];
annotation('textbox',dim,'String',txt,'FitBoxToText','on');
Furthermore I would like to have subscripted letters in the legend on the top right but I'm reading those out from the labels in a table. Is this possible?
Thanks!

Accepted Answer

Voss
Voss on 11 Aug 2023
Edited: Voss on 11 Aug 2023

To fix the one-line-text problem, enclose the relevant text in [ ] to make it a single character vector:

txt = {'Primary energy factors:',['Biomass =' num2str(Basisdaten.Primaerenergiefaktor.Biomasse.today)], ...
      ['Electricity =' num2str(Basisdaten.Primaerenergiefaktor.Strom.today)]};

More Answers (1)

C B
C B on 11 Aug 2023
Edited: C B on 11 Aug 2023
It is happening because its creating seprate cells.
you can modify like below :
txt = {'Primary energy factors:','Biomass = ' ,num2str(11), ...
'Electricity = ' num2str(12),};
display(txt)
txt = 1×5 cell array
{'Primary energy factors:'} {'Biomass = '} {'11'} {'Electricity = '} {'12'}
txt = {'Primary energy factors:',['Biomass = ' ,num2str(11)], ...
['Electricity = ', num2str(12)],};
display(txt)
txt = 1×3 cell array
{'Primary energy factors:'} {'Biomass = 11'} {'Electricity = 12'}
figure()
xlabel('Primary Energy Demand');
txt = {'Primary energy factors:',['Biomass = ' ,num2str(11)], ...
['Electricity = ', num2str(12)],};
dim = [.15 .6 .3 .3];
annotation('textbox',dim,'String',txt,'FitBoxToText','on');

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!