問題:matlab腳本與函數文件的中文註釋顯示亂碼.
環境:matlab R2016a.Windows 10 home.
解決方案:
step1
檢查locale值
matlab命令行鍵入命令
feature(‘locale’) html
>> feature('locale') ans = 包含如下字段的 struct: ctype: 'zh_CN.UTF-8' collate: 'zh_CN.UTF-8' time: 'zh_CN.UTF-8' numeric: 'en_US_POSIX.UTF-8' monetary: 'zh_CN.UTF-8' messages: 'zh_CN.UTF-8' encoding: 'UTF-8' terminalEncoding: 'UTF-8' jvmEncoding: 'UTF-8' status: 'MathWorks locale management system initialized.' warning: ''
注意:
System locale設置
控制面板Region,Administrative選項卡,Language for non-Unicode設置爲Chinese(Simplified,China).
step3
確認效果.
打開m文件,添加中文註釋,保存並從新打開確認中文顯示正常.
注意:
若是中文字符顯示?是亂碼問題,若是顯示方框則是字體顯示問題(好比使用西方字體顯示中文字體).可在matlab預設->字體選擇中文字體.matlab默認設置爲Monospaced,若是系統沒有支持等寬字體的中文字體,中文字符會顯示方框.能夠下載並安裝宋體-18030,該字體支持等寬字體.
補充
若是想以utf-8編碼,而不是GBK編碼的話,請編輯matlab的locale數據庫文件lcdata.xml(matlab bin目錄下).
刪除
< encoding name=」GBK」>
< encoding_alias name=」936」>
< /encoding>數據庫
並將
< encoding name=」UTF-8」>
< encoding_alias name=」utf8」/>
< /encoding>
改成< encoding name=」UTF-8」>
< encoding_alias name=」utf8」/>
< encoding_alias name=」GBK」/>
< /encoding>
重啓matlab以後,即以utf-8編碼.
注意:
常見的編碼格式
• ascII編碼,別名ANSI,使用7個bit.
• utf-8,可變長度,兼容ascII,中文字符佔16個bit
• utf-16,左右字符一概佔16個bit
• GB2312,簡體中文
• GB5,繁體中文
lcdata.xml修改locale tag不起做用,我猜想matlab硬編碼了locale列表,而不是查詢lcdata.xml.其內部的locale格式
locale — Format: language_territory.codeset
For example, for the U.S. English locale setting en_US.US-ASCII, en means that the display language is English. US indicates that time and date displays use U.S. conventions. US-ASCII is the coded character set (codeset) used to display text.jvm
From https://www.mathworks.com/help/matlab/matlab_env/how-the-matlab-process-uses-locale-settings.html
若是你的系統和用戶locale值均爲zh_CN,則matlab查詢硬編碼存儲的locale列表定位到zh_CN.GBK,並使用GBK在lcdata.xml查詢codeset.函數
參考文獻
• ISO-3166 Country Codes
• ISO-639 Language Codes
• Internationalization(matlab幫助)
• How do I get my MATLAB editor to read UTF-8 characters? UTF-8 characters in blank squares in editors, but in the command window and workspace works fine.字體