adding textbox to plot - text doesnt stay in one line

18 ビュー (過去 30 日間)
Jan Böttner
Jan Böttner 2023 年 8 月 11 日
コメント済み: Voss 2023 年 8 月 11 日
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)]};
  2 件のコメント
Jan Böttner
Jan Böttner 2023 年 8 月 11 日
perfect, thanks a lot!
Voss
Voss 2023 年 8 月 11 日
You're welcome!

サインインしてコメントする。

その他の回答 (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');
  1 件のコメント
Jan Böttner
Jan Böttner 2023 年 8 月 11 日
awesome, thanks a lot!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by