您的位置:首页 > 编程语言 > MATLAB

matlab中文显示乱码:控制台上的,编辑器的,图片中的

2018-01-11 17:27 513 查看
问题:matlab脚本与函数文件的中文注释显示乱码.
环境:matlab R2016a.Windows 10 home.
解决方案:
step1
检查locale值
matlab命令行键入命令
feature(‘locale’)

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

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: