How to align properly using sprintf?

I am trying to display information in subplot using sprintf to create messages. Sample code that I am using is below - even though left-justify and right-justify has constant values, the output doesn't look aligned.
messages = {};
messages{end+1} = sprintf(['\t%-20s',' : ','%10d'] , 'Feature 1', 152);
messages{end+1} = sprintf(['\t%-20s',' : ','%10d'], 'other feature', 6509);
messages{end+1} = sprintf(['\t%-20s',' : ','%10d'], 'N',3);
text(-0.15, 0.7, messages, 'FontWeight', 'bold');
axis off
Here is the screenshot of the output from running the above code:
I am hoping to have output that is better aligned, like below:
Feature 1 : 152
other feature : 6509
N : 3
Any help would be greatly appreciated!

 採用された回答

Rik
Rik 2018 年 3 月 12 日
移動済み: Voss 2023 年 12 月 14 日

0 投票

Using a fixed-width font will help if you're trying to align text with numbers of characters.

3 件のコメント

Louis
Louis 2018 年 3 月 12 日
移動済み: Voss 2023 年 12 月 14 日
Tried
text(-0.15, 0.7, messages, 'FontName', 'FixedWidth', 'FontWeight', 'bold');
but I get the same outcome.
*Fixed typo according to comment below
the cyclist
the cyclist 2018 年 3 月 12 日
移動済み: Voss 2023 年 12 月 14 日
It will work if you spell FixedWidth correctly. :-)
Louis
Louis 2018 年 3 月 12 日
移動済み: Voss 2023 年 12 月 14 日
Thanks for pointing that out. It works!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLabels and Annotations についてさらに検索

製品

質問済み:

2018 年 3 月 12 日

移動済み:

2023 年 12 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by