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:

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.
回答(1 个)
Walter Roberson
2019-9-20
0 个投票
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 个评论
Sagi Ravid
2019-9-20
Steven Lord
2019-9-20
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?
- Write the word mode, a colon, a tab, the number 145.5, a space, and the word nm.
- Move to the next line on the paper.
- Write the word mean, a colon, a tab, the number 175.3, a space, and the word nm.
- Move to the next line on the paper.
- 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.)
Walter Roberson
2019-9-20
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.
Walter Roberson
2019-9-21
Possibly with LaTex interpreter you could use \matrix
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!