是否可以比较完全用希腊字符书写的字符串? 例如:希腊字符的matlab字符串
str1='ΑΒΓΔ'
str2='ΕΖΗΘ'
strcmp(str1,str2)
是有可能当我从文件中读取希腊字符字符串应用上述? 例如:
line='ΑΒΓΔ;ΕΖΗΘ'
[str1 str2] = strread(line,'%s %s','delimiter',';')
strcmp(str1,str2)
是否可以比较完全用希腊字符书写的字符串? 例如:希腊字符的matlab字符串
str1='ΑΒΓΔ'
str2='ΕΖΗΘ'
strcmp(str1,str2)
是有可能当我从文件中读取希腊字符字符串应用上述? 例如:
line='ΑΒΓΔ;ΕΖΗΘ'
[str1 str2] = strread(line,'%s %s','delimiter',';')
strcmp(str1,str2)
字符串编码不是MATLAB的强项之一。使用Unicode时,有unicode2native
和native2unicode
。但是,就我所知,您在代码中输入的字符串文字仅为ASCII。这意味着您需要从使用fread
的合适编码的文件中读取非ASCII字符串,然后使用native2unicode
将原始字节转换为Unicode。
不可能使用fread,数据文件没有标准格式。我使用strread,然后我用分隔符分隔数据“;” – user3270686
@ user3270686:您可以先用'fread'读取您的数据,然后进行转换,然后进行进一步处理。但是所有进一步的处理都需要支持Unicode。 –
MATLAB和unicode 仍然还没有混合好,AFAIK。你的第一个例子可悲地返回假阳性。你的第二个例子有效,但并非没有警告。
例如test.txt
采取(使用UTF-8编码):
ΑΒΓΔ;ΕΖΗΘ
然后:
%// First try:
fid = fopen('test.txt', 'r', 'n', 'UTF-8');
A = textscan(fid, '%s', 'delimiter', ';');
fclose(fid);
A{1}{1}+0
%// apparently, textscan does not read the correct number of bits per
%// character...
%// Let's try manually:
fid = fopen('test.txt', 'r', 'n', 'UTF-8');
txt = fgetl(fid);
fclose(fid);
%// That makes things even worse! All characters get converted to the
%// placeholder character 26, causing false positives in string
%// comparisons:
D = textscan(txt, '%s', 'delimiter', ';');
D{1}{1}+0
strcmp(D{1}{2}, D{1}{2})
%// Regexp works better; it preserves the right character codes and yields
%// a (correct) negative on comparison:
C = regexp(txt, ';', 'split');
C{1}+0
strcmp(C{1}, C{2})
%// So, the "best" way:
fid = fopen('test.txt', 'r', 'n', 'UTF-8');
D = {};
while ~feof(fid)
line = fgetl(fid);
D{end+1} = regexp(line, ';', 'split'); %#ok<SAGROW>
end
fclose(fid);
的disp
仍然不能正确显示它们,除非你已特别选定的字体用于命令窗口/编辑窗口支持Unicode。
如果你在Linux上,如果你使用bash或类似的方式调用MATLAB,unicode显示器可以正常工作。但是,更多的是与你的shell比用MATLAB ...
而且,看着help unicode2native
:
%// Reading
fid = fopen('japanese.txt', 'r', 'n', 'Shift_JIS');
str = fread(fid, '*char')';
fclose(fid);
disp(str);
%// Writing
fid = fopen('japanese_out.txt', 'w', 'n', 'Shift_JIS');
fwrite(fid, str, 'char');
fclose(fid);
的disp
失败在这里(R2010a版本),但写是OK ...
看一看[此文件来自FEX](http://www.mathworks.com/matlabcentral/fileexchange/18956-read-unicode-files)。 –
除了@RodyOldenhuis说你可能还想阅读Amro的[这个答案](http://stackoverflow.com/a/21097643/983722)。以及[这个问题]的答案(http://stackoverflow.com/q/6863147/983722)。 –