Disabling printing underscore as subscript in figures

Underscores print as subscript in figures. Can I disable it because I want to print the underscores as well.
Thanks.

2 comentarios

Michael Marcus
Michael Marcus el 11 de Abr. de 2019
Editada: Stephen23 el 11 de Abr. de 2019
Although this allows underscores to print, it does not allow special symbols such as \mum to work.. Does anyone know how to allow both.
Mike Marcus
I did find out another way to keep the underscore. \_ does work ? I have answered my own question? Convert all underscores in the text to \_ instead of changing the interpreter to none.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 11 de Jun. de 2011
Editada: Image Analyst el 17 de En. de 2018
Set the Interpreter property for that field to 'none'; the default for text() fields is LaTex.
title('This_title has an underline', 'Interpreter', 'none'); % Also works with xlabel() and ylabel()

13 comentarios

AP
AP el 11 de Jun. de 2011
Thank you very much.
h=gco(1);
set(h,'text','none')
AP
AP el 11 de Jun. de 2011
Editada: Walter Roberson el 9 de Mayo de 2023
Why this does not work in the following for loop? :(
for i=1:4
subplot(2,2,i)
imshow(im{i})
h=gco;
set(h,'text','none');
title(sprintf('%s_%d',mytitle{i},i))
end
It does not apply the change in text property and prints the subscript.
Walter Roberson
Walter Roberson el 11 de Jun. de 2011
Editada: Walter Roberson el 9 de Mayo de 2023
Personally I never count on gco being the object I am interested in.
In the above, gco is likely to be the result of imshow(), but imshow() returns an image object, and image objects do not have a 'text' field.
When you title(), a _new_ text object is created to hold the title. That new text object is not going to inherit the properties of the old one.
I would suggest,
for i = 1:4
subplot(2,2,i)
imshow(im{i})
title(sprintf('%s_%d',mytitle{i},i), 'Interpreter', 'none');
end
AP
AP el 11 de Jun. de 2011
You're the best. Thanks.
Jan
Jan el 11 de Jun. de 2011
Another method: Replace '_' by '\_' in the string.
Hang Dong
Hang Dong el 17 de En. de 2018
Editada: Hang Dong el 17 de En. de 2018
Thank you, this works.
Addo
Addo el 13 de Feb. de 2018
How can I use this for the legend of a plot? I have a string with underscores that I would like to use for the legend. I don't want to change it to "\_" and 'Interpreter', 'none' doesn't work with legend('show').
AM
AM el 12 de Abr. de 2018
Editada: Walter Roberson el 12 de Abr. de 2018
try something like this:
leg=legend('data1','data2','data3');
set(leg,'Interpreter', 'none')
Walter Roberson
Walter Roberson el 12 de Abr. de 2018
Editada: Walter Roberson el 9 de Mayo de 2023
@AM is correct: although legend() does not accept that name/value pair, you can set it on the handle.
It looks like it does not work for stackedplot:
title(filename, 'Interpreter', 'none');
It returns error:
Error using matlab.graphics.chart.Chart/title
Too many input arguments specified when using title with stackedplot.
Is there a way to disable understcore in stackedplot title?
It appears that stackedplot treats titles differently. The great majority of plot types are within axes, and in those cases the axes has a Title property that is a text() object. But stackedplot() does not use axes: it is a direct parent of a figure, and the Title property for it is a character vector, with there being no Interpreter property.
It appears that you need to use the method suggested by @Jan in https://www.mathworks.com/matlabcentral/answers/9260-disabling-printing-underscore-as-subscript-in-figures#comment_20281 -- namely to replace the _ with \_
title(regexprep(filename, '_', '\\_'))
@Walter Roberson got it, thanks!
Eliot
Eliot el 16 de Abr. de 2025
Thank you for this easy solution to my messed-up plot titles!

Iniciar sesión para comentar.

Más respuestas (1)

HE
HE el 5 de Mayo de 2020
If you are using sprintf, \\_ should work for you.
old_cells = sprintf('Old cells: Y = %3.3f (X) \\^ %1.3f',coefs_old);
young_cells = sprintf('Young cells: Y = %3.3f (X) \\^%1.3f',coefs_young);

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

AP
el 11 de Jun. de 2011

Comentada:

el 16 de Abr. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by