How can I place the \, _, or ^ characters in a text command?

조회 수: 965 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2011년 11월 21일
댓글: Walter Roberson 2020년 5월 3일
I am using text commands: text, title, xlabel and ylabel, to label my plot. I want the ' \ ' (backslash) , ' _ ' (underscore) and ' ^ ' (caret) symbols to appear in the text, but whenever I place them in, it alters the text.
For example, typing ^t makes the "t" a superscript, _w makes "w" a subscript, and \theta makes a Greek character.
For instance, if I try to label my graph Experiment_6, the 6 is subscripted:
title('Experiment_6')

채택된 답변

MathWorks Support Team
MathWorks Support Team 2011년 11월 21일
MATLAB 5 introduced TeX characters for formatting text objects. The underscore character " _ " is interpreted by MATLAB as a subscript command. The backslash command " \" is interpreted by MATLAB to indicate that a TeX command is next. The caret character " ^" is interpreted by MATLAB as a superscript command.
In these examples, the interpreter is formatting the w as subscript, the t as superscript, and the \theta as a TeX command (which happens to draw the Greek letter Theta).
One way to bypass the interpreter and have the characters appear as typed, is to use the backslash character ( \ ) before the LaTeX command. For example:
\_w will make _w appear in your text
\^t will make ^t appear in your text
\\theta will make \theta appear in your text
Alternatively, you can set the "Interpreter" property of the text object to 'none'. For example:
title('Experiment_6','Interpreter','none')
or
t = title('Experiment_6')
set(t,'Interpreter','none')
To set the default Text 'Interpreter' property to 'none' so that no TeX character in a text string is interpreted at the root level, issue the following command:
set(0,'DefaultTextInterpreter','none')
For more information on TeX characters, refer to the following URL:
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 12월 6일
It is because the \ is special to sprintf() . You should use
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);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by