Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将unicode字符串写入Matlab中的文件_Matlab_Unicode_Utf 8 - Fatal编程技术网

将unicode字符串写入Matlab中的文件

将unicode字符串写入Matlab中的文件,matlab,unicode,utf-8,Matlab,Unicode,Utf 8,我有一个包含乌尔都语字符的字符串,比如“بجلی”,这是一个1x4数组。我想将其保存到一个文件中,以便在外部查看。虽然此字符串不会显示在主命令窗口中,但变量“str”会保存它。当我使用fprintf(fid,str)保存该文件并在记事本中打开该文件时,原始字符上会出现“箭头”。我可以轻松地将我的字符手动粘贴到记事本中。问题出在哪里?您需要使用fwrite()而不是fprintf(): 经核实: perl -E "open my $fh, q{<:utf8}, q{temp.txt}; wh

我有一个包含乌尔都语字符的字符串,比如“بجلی”,这是一个1x4数组。我想将其保存到一个文件中,以便在外部查看。虽然此字符串不会显示在主命令窗口中,但变量“str”会保存它。当我使用fprintf(fid,str)保存该文件并在记事本中打开该文件时,原始字符上会出现“箭头”。我可以轻松地将我的字符手动粘贴到记事本中。问题出在哪里?

您需要使用fwrite()而不是fprintf():

经核实:

perl -E "open my $fh, q{<:utf8}, q{temp.txt}; while (<$fh>) {while (m/(.)/g) {say ord $1}}"
1576
1580
1604
1740

perl-E“打开我的$fh,q{查找字符映射中的每个字符似乎很难。代码可以修改为以下代码:

fid = fopen('temp.txt', 'w');
str = char(['س','ل','ا','م');
encoded_str = unicode2native(str, 'UTF-8');
fwrite(fid, encoded_str, 'uint8');
fclose(fid);

这似乎更容易,但唯一的缺点是需要安装阿拉伯语/波斯语/乌尔都语等。

在文件中写入UTF-8字符串时,实际上没有必要避免使用
fprintf
。方法是正确打开文件:

f = fopen('temp.txt', 'w', 'native', 'UTF-8');
s = char([1576, 1580, 1604, 1740]);
fprintf(f, 'This is written as UTF-8: %s.\n', s);
fclose(f);

记事本使用特殊字符来确定文件的字符编码。您可能没有编写它们。这是记事本特有的一种奇怪行为。@Wug我刚刚使用了一个十六进制转储来确认它确实只向文件写入“1A”。Matlab显然认为这是该字符串的UTF-8 unicode表示形式,如unicode代码点查找(str,'UTF-8')。在线unicode代码点查找似乎不同意。
f = fopen('temp.txt', 'w', 'native', 'UTF-8');
s = char([1576, 1580, 1604, 1740]);
fprintf(f, 'This is written as UTF-8: %s.\n', s);
fclose(f);