将unicode字符串写入Matlab中的文件 [英] Write unicode strings to a file in Matlab
问题描述
我有一个字符串,其中包含诸如'بجلی'之类的乌尔都语字符,这是一个1x4数组.我想将其保存到文件中,以从外部查看.尽管此字符串未显示在主命令窗口中,但变量'str'确实将其保存.当我使用fprintf(fid,str)保存此文件,并在记事本中打开该文件时,原始字符上会出现箭头".我可以轻松地将我的角色手动粘贴到记事本中.问题出在哪里?
I have a string containing urdu characters like 'بجلی' this is a 1x4 array. I want to save this to a file, which would be viewed externally. Although this string doesnt display in the main Command Window, but variable 'str' does hold it. When I save this using fprintf(fid, str), and open that file in notepad there appear 'arrows' instead on the original characters. I can easily paste my characters into notepad manually. Where is the problem?
推荐答案
您需要使用fwrite()而不是fprintf():
You need to use fwrite() not fprintf():
fid = fopen('temp.txt', 'w');
str = char([1576, 1580, 1604, 1740, 10]);
encoded_str = unicode2native(str, 'UTF-8');
fwrite(fid, encoded_str, 'uint8');
fclose(fid);
已验证:
perl -E "open my $fh, q{<:utf8}, q{temp.txt}; while (<$fh>) {while (m/(.)/g) {say ord $1}}"
1576
1580
1604
1740
这篇关于将unicode字符串写入Matlab中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!