fprintf or fwrite non ASCII characters
Mostrar comentarios más antiguos
Hi everyone,
I am trying to write some strings on a text but some of them contain σ and μ and they are not written correctly in my file when I use either fprintf or fwrite. I tried also to open my file (fopen) with different encondings but nothing changed. Below you find my most significant attempts:
SheetName = 'example';
text_file = fopen(strcat(SheetName, '.txt'), 'w+','n','windows-1253');
fprintf(text_file,'%c', 'σ'); % prints ?
fprintf(text_file,'%s', char(963)); % prints ó
fprintf(text_file, '\x03c3'); % prints ó
fwrite(text_file, 'σ'); % prints ?
fwrite(text_file, char(963)); % prints ÿ
If I don't put the encoding 'windows-1253' I get even worse results: second and third line print a small square.
I have also read that it could depend on my OS but honestly I don't get why. Anyway, I have Windows 10 Pro.
Could you help me out with this? Thanks in advance!
6 comentarios
How are you checking the file content? It works for me:
fid = fopen('test.txt','wt');
fprintf(fid,'%c', 'σ');
fprintf(fid,'%s', char(963));
fprintf(fid, '\x03c3');
fwrite(fid, 'σ');
fwrite(fid, char(963));
fclose(fid);
type test.txt
double(fileread('test.txt'))
So far everything is working as expected. But we have no idea what tool/app you are using to check the file content.
Daniele Venanzetti
el 3 de Jun. de 2021
Stephen23
el 3 de Jun. de 2021
@Daniele Venanzetti: please show the ouput of the two commands that I used to check the file content.
Daniele Venanzetti
el 3 de Jun. de 2021
@Daniele Venanzetti: what OS are you using?
Perhaps you could try using these FOPEN options:
fopen('test.txt','w','n','UTF-8')
Daniele Venanzetti
el 3 de Jun. de 2021
Respuestas (1)
Constantino Carlos Reyes-Aldasoro
el 3 de Jun. de 2021
0 votos
Have you tried using TeX formatting? I.e. \alpha \sigma \mu
https://uk.mathworks.com/help/matlab/creating_plots/greek-letters-and-special-characters-in-graph-text.html
3 comentarios
Daniele Venanzetti
el 3 de Jun. de 2021
Have you tried using ascii values? e.g.
strcat('a=',181)
Daniele Venanzetti
el 4 de Jun. de 2021
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




