adding textbox to plot - text doesnt stay in one line

12 Ansichten (letzte 30 Tage)
Jan Böttner
Jan Böttner am 11 Aug. 2023
Kommentiert: Voss am 11 Aug. 2023
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!

Akzeptierte Antwort

Voss
Voss am 11 Aug. 2023
Bearbeitet: Voss am 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)]};

Weitere Antworten (1)

Chetan Bhavsar
Chetan Bhavsar am 11 Aug. 2023
Bearbeitet: Chetan Bhavsar am 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');

Kategorien

Mehr zu Labels and Styling finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by