2014-04-01 117 views
3

是否可以比較完全用希臘字符書寫的字符串? 例如:希臘字符的matlab字符串

str1='ΑΒΓΔ' 
str2='ΕΖΗΘ' 
strcmp(str1,str2) 

是有可能當我從文件中讀取希臘字符字符串應用上述? 例如:

line='ΑΒΓΔ;ΕΖΗΘ' 
[str1 str2] = strread(line,'%s %s','delimiter',';') 
strcmp(str1,str2) 
+0

看一看[此文件來自FEX](http://www.mathworks.com/matlabcentral/fileexchange/18956-read-unicode-files)。 –

+0

除了@RodyOldenhuis說你可能還想閱讀Amro的[這個答案](http://stackoverflow.com/a/21097643/983722)。以及[這個問題]的答案(http://stackoverflow.com/q/6863147/983722)。 –

回答

0

字符串編碼不是MATLAB的強項之一。使用Unicode時,有unicode2nativenative2unicode。但是,就我所知,您在代碼中輸入的字符串文字僅爲ASCII。這意味着您需要從使用fread的合適編碼的文件中讀取非ASCII字符串,然後使用native2unicode將原始字節轉換爲Unicode。

+0

不可能使用fread,數據文件沒有標準格式。我使用strread,然後我用分隔符分隔數據「;」 – user3270686

+0

@ user3270686:您可以先用'fread'讀取您的數據,然後進行轉換,然後進行進一步處理。但是所有進一步的處理都需要支持Unicode。 –

2

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 ...