MATLAB 源文件中的 Unicode 字符
我想在 注释中使用 Unicode 字符/wiki/MATLAB">MATLAB 源文件。当我写文字时,这似乎有效;但是,如果我关闭文件并重新加载它,“不寻常”的字符就会变成问号。我猜 MATLAB 正在将文件保存为 ASCII。
有没有办法告诉 MATLAB 使用 UTF-8 代替?
I'd like to use Unicode characters in comments in a MATLAB source file. This seems to work when I write the text; however, if I close the file and reload it, "unusual" characters have been turned into question marks. I guess MATLAB is saving the file as ASCII.
Is there any way to tell MATLAB to use UTF-8 instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据 http://www.mathworks.de/matlabcentral/newsreader/view_thread/238995
将更改编码为 UTF-8。您可以将上面的行放入您的startup.m 文件中。
According to http://www.mathworks.de/matlabcentral/newsreader/view_thread/238995
will change the encoding to UTF-8. You can put the line above in your startup.m file.
MATLAB 进程如何使用区域设置展示了如何设置不同平台的编码。使用
您可以在此处阅读有关此未记录函数的更多信息。另请参阅此 Matlab Central 线程 用于其他选项。
How the MATLAB Process Uses Locale Settings shows how to set the encoding for different platforms. Use
You can read more about this undocumented function here. See also this Matlab Central thread for other options.
仅限 Mac OSX!
当我找到适合我的情况的解决方案时,我想分享它。
Mathworks 建议此处使用
slCharacterEncoding(encoding) 以便根据需要更改编码,但对于 OSX,这并不能完全解决问题,因为接受的答案中的
feature('DefaultCharacterSet')
没有这样做。 MATLAB 答案中的以下链接帮助我获取用于打开和保存 .m 文件的 UTF-8 编码集:https://www.mathworks.com/matlabcentral/answers/12422- macosx-encoding-problem
Matlab 似乎忽略
slCharacterEncoding(encoding)
或feature('DefaultCharacterSet')
中设置的任何值,但使用系统偏好设置中设置的区域 - >语言与地区。检查在我们的例子中选择了哪个区域后,就可以在这个目录中的隐藏配置文件中定义实际编码,可以通过进入应用程序来打开此目录,然后右键单击Matlab,选择显示包内容,如屏幕截图所示(此处)德语)
例如,对于德国默认 ISO-8859-1,可以通过将文件 lcdata.xml 中的相应行更改
为
:如果所选区域不存在于lcdata.xml 文件这将不起作用。
希望这有帮助!
Mac OSX only!
As I found solution which worked in my case I want to share it.
Mathworks advises here to use
slCharacterEncoding(encoding)
in order to change the encoding as desired, but for the OSX this does not solve the issue exactly as thefeature('DefaultCharacterSet')
in accepted answer does not do it. What helped me to get the UTF-8 encoding set for opening and saving .m files was the following link on MATLAB answers:https://www.mathworks.com/matlabcentral/answers/12422-macosx-encoding-problem
Matlab seems to ignore any value set in
slCharacterEncoding(encoding)
orfeature('DefaultCharacterSet')
but uses region set in System Preferences -> Language & Region. After checking which region is selected in our case then it is possible to define the actual encoding in the hidden configuration file inThis directory can be opened by getting to the Applications and after right click on Matlab by selecting Show Package Contents as on screenshot (here in German)
For example for German default ISO-8859-1 it is possible to adjust it by changing the respective line in the file lcdata.xml:
to:
If the region which is selected is not present in the lcdata.xml file this will not work.
Hope this helps!
此处提供的解决方案适用于我在 Windows R2018a 上。
如果链接不起作用:想法是使用文件
matlabroot/bin/lcdata.xml
来配置编码名称的别名(可以在评论中的这个文件中找到一些解释):您可以使用自己的值而不是
windows-1252
,当前使用的编码可以通过运行feature('locale')
获取。但是,如果您在帮助注释中使用 Unicode 字符,帮助浏览器将无法识别它们,控制台窗口输出也是如此。
The solution provided here worked for me on Windows with R2018a.
In case link doesn't work: the idea is to use file
matlabroot/bin/lcdata.xml
to configure an alias for encoding name (some explanation can be found in this very file in the comments):You would use your own value instead of
windows-1252
, currently used encoding can be obtained by runningfeature('locale')
.Although, if you use Unicode characters in help comments, the help browser does not recognize them, as well as console window output.
对于 Mac OS 用户,Jendker 的解决方案确实有帮助!首先非常感谢。
在这里回顾一下。
通过在命令窗口中键入
getenv('LANG')
来检查 Matlab 中的默认语言。我的返回了en_US.ISO8859-1
。在Application目录中找到Matlab,显示其包内容。转到
bin
,以管理员身份打开lcdata.xml
,找到相应的xpg_name
,在我的例子中为en_US.ISO8859-1< /代码>。将同一行中的
encoding
更改为UTF-8
。保存。重新启动Matlab,一切就完成了!
For Mac OS users, Jendker's solution really helps!!! Thanks a lot first.
Recap here.
Check the default language in Matlab by typing in the command window
getenv('LANG')
. Mine returneden_US.ISO8859-1
.In the Application directory find Matlab, show its package contents. Go to
bin
, openlcdata.xml
as an administrator, locate the correspondingxpg_name
, in my caseen_US.ISO8859-1
. Changeencoding
in the same line toUTF-8
. Save it.Restart Matlab, and it's all done!