Saturday, March 2, 2013

abnormal characters when using matlab to read txt files

when I read Chinese txt files using fopen function on matlab, many abnormal characters appear while there is no problem using the same code on the other PC. I know it is because of the default system encoding and decoding setting. It should be done if you configure it through control panel.

however, you can also solve the problem from the angle of Matlab, just still use fopen func, only difference is to add one or two parameters which we seldom pay attention to.

fileID = fopen(filename, permission, machineformat, encoding)

You can choose one of the following option for the 4th parameter encoding in above fopen function according to your situation.


'Big5'         'SO-8859-1'    'windows-932'
'EUC-JP'       'ISO-8859-2'   'windows-936'
'GBK'          'ISO-8859-3'   'windows-949'
'Macintosh'    'ISO-8859-4'   'windows-950'
'Shift_JIS'    'ISO-8859-9'   'windows-1250'
'US-ASCII'     'ISO-8859-13'  'windows-1251'
'UTF-8'        'ISO-8859-15'  'windows-1252'  
                              'windows-1253' 
                              'windows-1254' 
                              'windows-1257'
For my case, I choose the last one, 'UTF-8', and it works!

No comments:

Post a Comment