Printing special characters on screen and in file
18 views (last 30 days)
Show older comments
If you type fprintf(1,'%s\n',197) you get an Angstrom character on the screen. If you write the same thing to a file you do not see the Angstrom symbol when you open the file with MSWord or BBedit you see an approximately equal symbol.
You need to use 129 to print an Anstrom character in a file rather then 197 (see code). This occurs for other special characters also. Why?
fid=fopen('test.txt');
fprintf(fid,'%s\n',129);
fclose(fid);
0 Comments
Answers (1)
Walter Roberson
on 19 Jan 2017
fid=fopen('test.txt', 'w'); %code fix
fprintf(fid,'%s\n',129);
fclose(fid);
When you open this file with MS Word, it will bring up a file conversion dialog that says "Select the encoding that makes your document readable". You report that 197 shows up as "approximately equal" and that 129 shows up as angstrom. That particular combination of characters tells us that you must have selected "Croatian (Mac)" or "Icelandic (Mac)" or "Romanian (Mac)" or "Turkish (Mac)" or "Western European (Mac)" as the translation. You should instead select "Western European (ISO)".
Alternately, you could write your characters out properly:
fid = fopen('test.txt', 'w', 'ieee-le','UTF-8');
fprintf(fid, '%s\n', [197 129]);
fclose(fid)
This will write out the document UTF-8 encoded. When you open the test.txt file in MS Word, it will still give you a conversion box, but it will suggest Unicode (UTF-8) which is what you should select.
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!