日本語文字列を読み込んで表示すると、文字化けするのはなぜですか?
36 views (last 30 days)
Show older comments
MathWorks Support Team
on 25 Oct 2013
Answered: MathWorks Support Team
on 25 Oct 2013
日本語文字列を読み込んで表示すると、文字化けが発生します。
Accepted Answer
MathWorks Support Team
on 25 Oct 2013
MATLAB 7.0(R14) から文字コードの扱いが変更され、Unicode対応となりました。このため、Shift_JISキャラクタ(日本語文字)等のUnicode以外の文字コードを含むファイルを読み込んで表示する場合などに、文字化けが発生します。
MATLAB 7.0(R14)以降で、正しい日本語文字列として認識するためには、以下のようにnative2unicode関数を使用します。
native2unicodeは、Shift_JISキャラクタをUnicodeキャラクタに変換します。
例)
>> str = 'あいうえお';
>> word = sscanf(str,'%s')
word =
・・・・・・・|・¨
>> native2unicode(word)
ans =
あいうえお
native2unicode関数の詳細につきましては、下記コマンドによりヘルプドキュメントをご参照ください。
>> help native2unicode
>> doc native2unicode
上記例のsscanfの他にも、fgetlやfgets、fscanf等、native2unicodeによって、明示的にUnicodeへ変換しない場合に文字化けが発生する関数があります。この場合には、以下のように FOPEN コマンド実行時に、ファイルエンコーディングを指定することで回避できる場合があります。
>> fid = fopen('mydata.txt','r','n','UTF-8'); % UTF-8 エンコードの場合
0 Comments
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!