How do I add a hat on a character which is displayed in the LEGEND of a figure in MATLAB 7.0 (R14)?

98 views (last 30 days)
I would like to add a hat on a character in a string which is displayed in the legend of a figure in MATLAB 7.1 (R14SP3).

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 12 Dec 2012
As of MATLAB 7.0 (R14), "LaTeX" has been added as an optional interpreter for text objects in a figure.
In order to put a hat on a character that is to be displayed within a figure, the "Interpreter" property of the text should to be set to "LaTeX" and the equivalent LaTeX command "\hat{x}" should be typed as "$$\hat{x}$$".
The legend of a figure is an axes object. The text object in the legend is a child of the parent axes object. The following code demonstrates how a 'hat' can be added with the LEGEND function:
% Plot a line
plot(1:10);
% Create a blank legend
h = legend('');
% hchild is the handle to the children of the axes object.
% In this case it is a 3 by 1 vector and hchild(3) is the
% handle to the string in the legend
hchild = get(h,'children');
% Set LaTeX as the interpreter
set(hchild(3),'Interpreter','LaTeX');
% Add x with a hat to the legend in the current figure
set(hchild(3),'String','$$\hat{x}$$');
  1 Comment
dpb
dpb on 23 Apr 2020
While old, just stumbled across... "$\hat{x}$ is not working"
Use
$$\hat{x}$$
NB: the double $$
Also, legend is no longer an axis but a legend object that wraps the axes inside that no longer have total access to the underlying axis itself...

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!