Tab formatter is not working with sprintf
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Walter Roberson
le 20 Sep 2019
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 commentaires
Walter Roberson
le 20 Sep 2019
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
le 21 Sep 2019
Possibly with LaTex interpreter you could use \matrix
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!