Tab formatter is not working with sprintf
6 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
Walter Roberson
2019 年 9 月 20 日
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 件のコメント
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
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!