Tab formatter is not working with sprintf

조회 수: 8 (최근 30일)
Sagi Ravid
Sagi Ravid 2019년 9월 20일
댓글: Walter Roberson 2019년 9월 21일
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.

답변 (1개)

Walter Roberson
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
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by