フィルターのクリア

how to add percentage symbol (%) in a label

104 ビュー (過去 30 日間)
alessandro marino
alessandro marino 2023 年 10 月 15 日
コメント済み: Star Strider 2023 年 10 月 15 日
Hi,
I would like to plot a bargraph and add labels to the bars, showing a numerical value followed by the percentage symbol (%). Please find below a MWE:
x=table{:,"Xvalues"};
y=table{:,"Yvalues"};
barGraph=bar(x,y,0.9);
xtips1=barGraph(1).XEndPoints;
ytips1=barGraph(1).YEndPoints;
labels1=sprintfc('%.2f',table.labels);
text(xtips1,ytips1,labels1,'Rotation',45);
I know I can do that by defining 'labels1' as a string and then adding ' + '%' ' into the text command, as in:
x=table{:,"Xvalues"};
y=table{:,"Yvalues"};
barGraph=bar(x,y,0.9);
xtips1=barGraph(1).XEndPoints;
ytips1=barGraph(1).YEndPoints;
labels1=string(table.labels);
text(xtips1,ytips1,labels1 + '%','Rotation',45);
in this case, though, I cannot cut the values in 'labels1' after the second decimanl digit.
Thanks
  2 件のコメント
Matt J
Matt J 2023 年 10 月 15 日
We do not have your "table" variable and so cannot run the example. Also, "table" is inadvisable as a name for the varable, since it is also the name of a command.
alessandro marino
alessandro marino 2023 年 10 月 15 日
Thanks for pointing this out, I used "table" as a variable only to show exactly how I had structured my code. I meant it to represent a generic table containing values. I wanted to make sure the error did not come from a wrong way of extracting the values. Sorry about it!

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

採用された回答

Star Strider
Star Strider 2023 年 10 月 15 日
The approach seems to work here using synthetic data —
% x=table{:,"Xvalues"};
% y=table{:,"Yvalues"};
x = 1:5;
y = round(rand(1,5)*100);
barGraph=bar(x,y,0.9);
labels = y;
xtips1=barGraph(1).XEndPoints;
ytips1=barGraph(1).YEndPoints;
% labels1=string(table.labels);
labels1 = string(labels);
text(xtips1,ytips1,labels1 + '%','Rotation',45);
If this does not work with your data, you will need to supply it.
.
  2 件のコメント
alessandro marino
alessandro marino 2023 年 10 月 15 日
Thank you @Star Strider, nice and quick solution!
Star Strider
Star Strider 2023 年 10 月 15 日
As always, my pleasure!
Thank you!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by