Tab formatter is not working with sprintf

I am trying to write text to put in as an annotation on a line plot. Unfortunately, my code is not working as I'd like it, even though all of the documentation indicates that it should. This is not the very time I have not been able to use the tab formatter on MATLAB.
This is my code:
figure
plot(sizes(:, 1), count1(:, 1), 'LineWidth', 2);
lsline
dim = [0.3 0.6 .3 .3];
str = sprintf('mode:\t%5.1f nm\nmean:\t%5.1f nm\nSD:\t%5.1f nm', modeSize, meanSize, stdDev);
annotation('textbox',dim,'String', str,'FitBoxToText','on');
I expect the text in the textbook to show up as:
mode: 145.5 nm
mean: 175.3 nm
SD: 66.8 nm
However, my plots looks like this:
Example.jpg
The only explanation I can come up with is that I specified the text as 'String' when I created the annotation. This is does not make sense though as 'sprintf' creates character vectors and strings.

Answers (1)

Fonts. As in the default is variable-spaced fonts. And tab does not have a particular width.
tab is a concept for character-oriented displays like the command window, not for graphics display.
If you want to align text, then create a separate text() or annotation() object for the part to be aligned, and set appropriate alignment properties.

4 Comments

Why did the the new line formatter work?
Sagi Ravid,
If I asked you to write (with pencil and paper) exactly what I describe in the five steps below, and if I asked Walter Roberson to do the same, will they be exactly aligned?
  1. Write the word mode, a colon, a tab, the number 145.5, a space, and the word nm.
  2. Move to the next line on the paper.
  3. Write the word mean, a colon, a tab, the number 175.3, a space, and the word nm.
  4. Move to the next line on the paper.
  5. Write the word SD, a colon, a tab, the number 66.8, a space, and the word nm.
You and Walter will likely leave different amounts of space between the colons and the numbers. Your tabs (and likely your words, your numbers, and the space after the numbers) will be different widths because your "fonts" (your handwriting) are different.
If your paper is lined, you're likely going to agree on what "the next line on the paper" is (though if you're using different paper, the distance between each line might be different.)
The linefeed character appears to be processed by the tex interpreter. You can also use \newline in place of the newline character -- but be careful that you do not use \newline inside of an sprintf format or the \n will be translated
str = sprintf('mode:\t%5.1f nm\\newlinemean:\t%5.1f nm\\newlineSD:\t%5.1f nm', modeSize, meanSize, stdDev);
If you switch to latex interpreter then you can use various spacing commands such as \hspace or \quad . You could probably use the array constructors too, but I seem to be having some difficulty finding the right ones at the moment.

Sign in to comment.

Categories

Products

Release

R2019a

Asked:

on 20 Sep 2019

Commented:

on 21 Sep 2019

Community Treasure Hunt

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

Start Hunting!