Printing special characters on screen and in file

17 次查看(过去 30 天)
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);

回答(1 个)

Walter Roberson
Walter Roberson 2017-1-19
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.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by