MATLAB: How to add a hat on a character which is displayed in the LEGEND of a figure in MATLAB 7.0 (R14)

label;legendMATLABtexwedge

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).

Best Answer

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}$$');