日本語文字列を読み込​んで表示すると、文字​化けするのはなぜです​か?

35 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2013 年 10 月 25 日
回答済み: MathWorks Support Team 2013 年 10 月 25 日
日本語文字列を読み込んで表示すると、文字化けが発生します。

採用された回答

MathWorks Support Team
MathWorks Support Team 2013 年 10 月 25 日
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 件)

カテゴリ

Help Center および File Exchange文字と文字列 についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!