How to display the celcius sumbol (°C) using fprint??

I am creating an output file using fprint and want to display the unit of temperature in Celcius. Anyone knows how to do this?

 Réponse acceptée

Stephen23
Stephen23 le 23 Juin 2015
Modifié(e) : Stephen23 le 4 Déc 2019
Here are four methods:
fid = fopen('temp.txt','wt');
fprintf(fid,'symbol one: °C\n');
fprintf(fid,'symbol two: %cC\n',176);
fprintf(fid,'symbol three: %cC\n',char(176));
fprintf(fid,'symbol four: \260C\n');
fclose(fid);
And the generated file:
symbol one: °C
symbol two: °C
symbol three: °C
symbol four: °C
EDIT: Added octal syntax based on Walter Roberson's comment.

7 commentaires

Fotis
Fotis le 23 Juin 2015
Well thank you all. But none of these actually work! I have this code fprintf(fileID,'Ambient Temperature \t\t\t [] \t : \t %0.1f \n',T.amb-273.15) and I want the symbol only to be placed in between the brackets
Stephen23
Stephen23 le 23 Juin 2015
Modifié(e) : Stephen23 le 23 Juin 2015
Here are those three methods, but using your format string:
T.amb = 300;
fid = fopen('temp.txt','wt');
fprintf(fid,'Ambient Temperature \t\t\t [°] \t : \t %0.1f \n',T.amb-273.15);
fprintf(fid,'Ambient Temperature \t\t\t [%c] \t : \t %0.1f \n',176,T.amb-273.15);
fprintf(fid,'Ambient Temperature \t\t\t [%c] \t : \t %0.1f \n',char(176),T.amb-273.15);
fclose(fid);
And the output text file:
Ambient Temperature [°] : 26.9
Ambient Temperature [°] : 26.9
Ambient Temperature [°] : 26.9
Do you get a different result? What locale setting do you have? Are you specifying the file encoding? How are you opening and looking at the file?
Fotis
Fotis le 23 Juin 2015
Thank you!!
Stephen23
Stephen23 le 23 Juin 2015
My Pleasure!
Note that it is also considered polite to Accept answers that resolve your question. You do not seem to have done this for any of your questions.
Another couple of variations:
fprintf(fid,'Ambient Temperature \t\t\t [\260] \t : \t %0.1f \n',T.amb-273.15);
fprintf(fid,'Ambient Temperature \t\t\t [\xB0] \t : \t %0.1f \n',T.amb-273.15);
Caution: If you were trying to produce the output °C then \260C will work but \xB0C will not, as it will not be able to tell that the C is not intended to be part of the hex sequence.
Out of curiosity, where do the codes in Walter's helpful comment come from? I knew that the degree symbol could be produced by Alt + 0176 so it didn't surprise me that passing 176 to a %c in fprintf gave me the right symbol.
>> fprintf('%c\n',176)
°
However, for my particular application, I can avoid needing to add a cumbersome workaround if I could put the symbol right in the format string. But these didn't give me what I expected:
>> fprintf('\176\n')
~
>> fprintf('\x176\n')
Ŷ
>> fprintf('\0176\n')
~
>> fprintf('\x0176\n')
Ŷ
Obviously using the code Walter suggested worked, but how did you know the degree symbol was \260?
>> fprintf('\260\n')
°
Stephen23
Stephen23 le 4 Déc 2019
Modifié(e) : Stephen23 le 4 Déc 2019
"but how did you know the degree symbol was \260?"
Simply convert decimal to octal: 176 (decimal) = 260 (octal).
The fprintf documentation give two syntaxes for specifying any unicode character directly:
  • using hexadecimal '\xN'
  • using octal '\N'
The value 176 is decimal, so it cannot be used directly with either of those syntaxes, but if you convert it to the correct base it it will work exactly as documented (although watch out for the catch that Walter Roberson showed when using hexadecimal with a trailing 'C' character).

Connectez-vous pour commenter.

Plus de réponses (1)

Ingrid
Ingrid le 23 Juin 2015
first search the previous answers and you will find what you are looking for: http://www.mathworks.com/matlabcentral/answers/32376-degree-symbol
I prefer to use:
^{\circ}

2 commentaires

Stephen23
Stephen23 le 23 Juin 2015
Modifié(e) : Stephen23 le 23 Juin 2015
The syntax ^{\circ} applies to MATLAB text objects (e.g. title, etc) using the TeX or LaTeX interpreters, not to fprintf as the OP is asking about.
\circ is also a work-around. I am told that publications do not like \circ as it is not considered a correct degree symbol. I seem to recall it is not raised at the correct height for a proper degree symbol.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Scripts dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by