adding textbox to plot - text doesnt stay in one line

15 次查看(过去 30 天)
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!

采纳的回答

Voss
Voss 2023-8-11
编辑:Voss 2023-8-11

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)]};

更多回答(1 个)

C B
C B 2023-8-11
编辑:C B 2023-8-11
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');

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by