MATLAB: How to display the degree symbol on a figure using the SPRINTF function

MATLAB

I would like to use the degree symbol in one of the text objects that I am placing on my figure. This object is represented by the \circ escape sequence. I can use it well in the following manner:
text(1,1, '45\circ');
However, I have additional variables that I need to insert in my string expression for which I need to use the SPRINTF function as follows:
text(1,1, sprintf('%d\circ', x))
% where x = 45
The SPRINTF function however does not accept the \circ operator and gives a warning. The XLABEL, YLABEL, TEXT and other functions all accept this escape sequence.

Best Answer

The SPRINTF function in MATLAB does not support the \circ escape sequence for the degree symbol as this is not defined by the C standard with which this function has been modeled.
To work around this issue, you can perform one of the following:
1. Use the ASCII value of the degree symbol (176) in conjunction with the CHAR function within SPRINTF as follows:
s = sprintf('45%c', char(176));
Note that different fonts interpret ASCII values differently and you should verify this value before using it.
2. Use NUM2STR to create the desired string and then pass into the TEXT command.
x= 45;
text(1,1, [num2str(x) '\circ'])
Related Question