How to align properly using sprintf?

30 views (last 30 days)
Louis
Louis on 12 Mar 2018
Moved: Voss on 14 Dec 2023
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!

Accepted Answer

Rik
Rik on 12 Mar 2018
Moved: Voss on 14 Dec 2023
Using a fixed-width font will help if you're trying to align text with numbers of characters.
  3 Comments
the cyclist
the cyclist on 12 Mar 2018
Moved: Voss on 14 Dec 2023
It will work if you spell FixedWidth correctly. :-)
Louis
Louis on 12 Mar 2018
Moved: Voss on 14 Dec 2023
Thanks for pointing that out. It works!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!